我在Spring MVC REST服务中使用JPA和Hibernate进行持久化,parent实体和child实体之间存在一对多的双向关系(即.parent有一个或多个子节点,子节点只有一个父节点).
每当我尝试返回JSON中的父实体列表时,我会在无限循环中得到如下内容:
[{"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"
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError)
以下是我的控制器:
@RequestMapping(value="/getBusinesses", method = RequestMethod.GET)
@ResponseBody
public List<Business> getAllBusinessTypes(){
List<Business> businesses = businessService.findAllBusinesses();
return businesses;
}
Run Code Online (Sandbox Code Playgroud)
我的2个实体是:
@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 …Run Code Online (Sandbox Code Playgroud)