1

I got a problem with a maven Spring project, started with Springboot. My problem is when I call a method of a controller, it returns some fields that I don't want.

For example, "Pays" (=country) is related to "Departement" (Department) with a OneToMany (one Pays got many Departements)

Here's my Entity :

@Entity
@Table(name = "pays")
public class Pays {

    public interface DefaultView {}

    @Id
    @JsonView(DefaultView.class)
    private Long id;
    @JsonView(DefaultView.class)
    private String nom;

    @OneToMany(mappedBy = "pays")
    private List<Departement> departements;

    public Pays(Long id, String nom) {
        this.id = id;
        this.nom = nom;
    }

    public Pays() {
    }

    public Long getId() {
        return id;
    }

    public String getNom() {
        return nom;
    }

    public List<Departement> getDepartements() {
        return departements;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public void setDepartements(List<Departement> departements) {
        this.departements = departements;
    }
}

and this controller :

@RestController
@RequestMapping(value = "/ws/pays")
public class PaysController {

    @Autowired
    private PaysService paysService;

        @RequestMapping("/default")
        @JsonView(Pays.DefaultView.class)
        public List<Pays> getPayss() {
            return paysService.findAll();
        }
}

Everytime I go to /ws/pays/default - and call "getPayss()" -, it makes an infinite loop, trying to call the "Departements" of the "Pays", and the "Pays" of each "Departement", and so on...

For this example, I don't need the "Departements" of a "Pays", so I could use @JsonIgnore, but I don't want to, because for other calls, I will need the "Pays" of a "Departement"

Does anyone has a solution to fetch and serialize only the "id" and "nom" of my object "Pays" for this example ?

FYI, here's a sample of my Entity "Departement" :

@Entity
@Table(name = "departement")
public class Departement {

    @Id
    private Long id;
    private String nom;
    private String code;
    private String soundex;

    @ManyToOne
    @JoinColumn(name = "id_pays")
    private Pays pays;

    //Getters and setters are properly declared, but not shown here to preserve space
}

Thanks a lot

bidy
  • 13
  • 2

1 Answers1

1

Take a look with: Infinite Recursion with Jackson JSON and Hibernate JPA issue

Your problem has been described and solved with above link.

Community
  • 1
  • 1
Paweł Głowacz
  • 2,926
  • 3
  • 18
  • 25
  • 1
    Thank you for this answer, but I've already read this link. One of the solution is to use "@JsonIgnore" in the Entity, but I don't want to, because I need to use some properties on other calls. I wonder if we could do that with "@JsonView" ? – bidy Aug 07 '15 at 09:40
  • Have you read about `@JsonManagedReference` and `@JsonBackReference` ? – Paweł Głowacz Aug 07 '15 at 11:16
  • Yes I've read that, but the problem is the following : Sometimes I'll want to get the "Pays" and not the "Departements", and some other times I'll want to get the "Departements" and not the "Pays". Sometime i want to fetch the owner and all his items, but with the infinite loop i'll also get the owner of each items, etc. And sometimes i want to fetch all the items and their owner. I want to to this dynamically, according to the controller i use. Whereas the "JSONManage/BackReference is used on the Entity, so it doesn't depend on the use case. – bidy Aug 29 '15 at 21:50