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

时间:2014-01-17 03:16:25

标签: hibernate rest spring-mvc jpa jackson

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

每当我尝试返回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或我是愚蠢和遗漏明显的东西?

1 个答案:

答案 0 :(得分:15)

我找到了解决方案here

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

我必须将@JsonManagedReference注释添加到我的Business对象中的促销列表的getter中(我的OneToMany关系中的'one'),如下所示:

@Entity
public class Business implements Serializable{ 

    ...

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

和@JsonBackReference我的Promotion对象中的业务对象的getter(我的OneToMany关系中的'many'),如下所示:

@Entity
public class Promotion { 

    ...

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

似乎这种双向关系会导致Jackson的序列化问题。

也必须使用Jackson 1.6或更高版本。