Spring MVC REST JPA Hibernate Jackson nieskończona rekurencja błąd JSON jeden-do-wielu
Mam dwukierunkową relację jeden do wielu między aparent
podmiot ichild
encje (tj ..parent mają jedno lub więcej dzieci i dziecko ma tylko jednego rodzica) w usłudze REST Spring MVC, która używa JPA i Hibernate do trwałości.
Ilekroć próbuję zwrócić listę jednostek macierzystych w JSON, otrzymuję coś w następujący sposób w nieskończonej pętli:
[{"businessName":"Cake Shop","businessDescription":"We sell cakes","businessId":1,"promotions":[{"name":"Cake Sale","id":1,"description":"Get free cakes","business":{"businessName":"Cake Shop","businessDescription":"We sell cakes","businessId":1,"promotions":[{"name":"Cake Sale","id":1,"description":"Get free cakes","business"
z następującym błędem:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
Poniżej jest mój kontroler:
@RequestMapping(value="/getBusinesses", method = RequestMethod.GET)
@ResponseBody
public List<Business> getAllBusinessTypes(){
List<Business> businesses = businessService.findAllBusinesses();
return businesses;
}
a moje 2 podmioty to:
@Entity
public class Business implements Serializable{
@Id
@GeneratedValue
private Long businessId;
private String businessName;
private String businessDescription;
@OneToMany(mappedBy = "business", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Promotion> promotions = new ArrayList<Promotion>();
public String getBusinessDescription() {
return businessDescription;
}
public void setBusinessDescription(String businessDescription) {
this.businessDescription = businessDescription;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public Long getBusinessId() {
return businessId;
}
public void setBusinessId(Long businessId) {
this.businessId = businessId;
}
public List<Promotion> getPromotions() {
return promotions;
}
public void setPromotions(List<Promotion> promotions) {
this.promotions = promotions;
}
}
i
@Entity
@Table(name = "promotions")
public class Promotion implements Serializable{
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Business business;
private String name;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Business getBusiness() {
return business;
}
public void setBusiness(Business business) {
this.business = business;
}
}
Mam włączonego Jacksona, czy nie powinien automatycznie przekonwertować JSON, czy też jestem głupi i brakuje mi czegoś oczywistego?