【问题标题】:Spring MVC REST JPA Hibernate Jackson infinite recursion one-to-many JSON errorSpring MVC REST JPA Hibernate Jackson 无限递归一对多 JSON 错误
【发布时间】:2014-01-17 03:16:24
【问题描述】:

在使用 JPA 和休眠以保持持久性。

每当我尝试以 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;
    }
}

我已经包含了 Jackson,它应该不会自动转换 JSON 还是我很愚蠢并且遗漏了一些明显的东西?

【问题讨论】:

    标签: hibernate rest spring-mvc jpa jackson


    【解决方案1】:

    我找到了解决方案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 注释添加到我的业务对象(我的 OneToMany 关系中的“一个”)中的促销列表的 getter 中,如下所示:

    @Entity
    public class Business implements Serializable{ 
    
        ...
    
        @JsonManagedReference
        public List<Promotion> getPromotions() {
            return promotions;
        }
    

    和@JsonBackReference 对我的 Promotion 对象(我的 OneToMany 关系中的“许多”)中的业务对象的 getter 像这样:

    @Entity
    public class Promotion { 
    
        ...
    
        @JsonBackReference
        public Business getBusiness() {
            return business;
        }
    

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

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

    【讨论】:

    • 任何序列化框架都存在双向关系的问题。我不知道杰克逊支持这个:-)。
    • 谢谢!你拯救了我的一天。
    • 很好的答案!我们通常使用lombok getter,所以你可以把这个Json注解放在字段上,而不是getter。请记住删除您拥有的 @JsonIgnore 案例,因为它会在您的请求中为您提供不受支持的媒体类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2018-10-02
    • 2012-02-20
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多