相关疑难解决方法(0)

Spring MVC REST JPA Hibernate Jackson无限递归一对多JSON错误

我在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)

rest hibernate jpa spring-mvc jackson

3
推荐指数
1
解决办法
9036
查看次数

标签 统计

hibernate ×1

jackson ×1

jpa ×1

rest ×1

spring-mvc ×1