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


问题内容

我在使用JPA和Hibernate进行持久化的Spring MVC
REST服务中,parent实体与child实体之间存在一对多的双向关系(即,父母有一个或多个孩子,孩子只有一个父母)。

每当我尝试在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"

出现以下错误:

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;
}

我的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 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;
   }

}

@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;
    }
}

我有杰克逊,应该不会自动转换JSON,还是我很愚蠢并且缺少明显的东西?


问题答案:

我在这里找到解决方案

http://fasterxml.github.io/jackson-
annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonManagedReference.html

https://fasterxml.github.io/jackson-
annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/JsonBackReference.html

我必须在业务对象(OneToMany关系中的“一个”)的促销活动列表的getter中添加@JsonManagedReference批注,如下所示:

@Entity
public class Business implements Serializable{

    ...

    @JsonManagedReference
    public List<Promotion> getPromotions() {
        return promotions;
    }

和@JsonBackReference到我的Promotion对象(我​​的OneToMany关系中的“很多”)中的业务对象的吸气剂,如下所示:

@Entity
public class Promotion {

    ...

    @JsonBackReference
    public Business getBusiness() {
        return business;
    }

看来这种双向关系导致了Jackson的序列化问题。

另外,必须使用Jackson 1.6或更高版本。