如何在不陷入无限的反序列化循环的情况下,将一个关系反序列化为多个关系

时间:2021-05-13 02:49:23

here is my Product class

这是我的产品类别。

@Entity
public class Product {

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="listingGroup_id")
    @JsonBackReference
    public ListingGroup listingGroup;

and here is my groupProduct class

这是我的groupProduct类。

@Entity
public class GroupProduct {

    @OneToMany(mappedBy = "listingGroup", fetch = FetchType.EAGER)
    @JsonManagedReference
    Set<Product> products;

GAOL:

*:

  1. When I query for product, I want product with ProductGroup (ProductGroup should not serialize again the products inside)
  2. 当我查询产品时,我想要product with ProductGroup (ProductGroup不应该再次序列化内部的产品)
  3. When I query GroupProduct, I want the products inside (without these list of products each including again the GroupProduct)
  4. 当我查询GroupProduct时,我希望产品在其中(没有这些产品列表,每个都包含GroupProduct)

ALREADY TRIED

已经试过

  1. JsonBackReference, JsonManagedReference:

    JsonBackReference JsonManagedReference:

    The GroupProduct get everything fine, but

    团购产品一切都很好,但是

    Problem : the deserialized products does not contain the group Product : {id: 1, ... groupProduct: null}

    问题:反序列化产品不包含组产品:{id: 1,…groupProduct:零}

  2. JsonIdentityInfo : I am not able anymore to deserialize the objects java.lang.IllegalArgumentException: No converter found for return value of type...

    JsonIdentityInfo:我再也不能反序列化对象java.lang了。IllegalArgumentException:没有找到返回值类型的转换器…

Environment

环境

  • spring boot 1.5.8
  • 弹簧引导1.5.8
  • hibernate 5.0.12
  • hibernate 5.0.12
  • 'jackson-annotations', version: '2.8.0'
  • “jackson-annotations”,版本:“2.8.0”
  • 'jackson-databind', version: '2.8.7'
  • “jackson-databind”,版本:“2.8.7”

1 个解决方案

#1


1  

I think you need @JsonIgnoreProperties annotation, like this:

我认为您需要@JsonIgnoreProperties注释,如:

@JsonIgnoreProperties("products")
public ListingGroup listingGroup;

or like this:

或者像这样:

@JsonIgnoreProperties("listingGroup")
Set<Product> products;

or both.

或两者兼而有之。

#1


1  

I think you need @JsonIgnoreProperties annotation, like this:

我认为您需要@JsonIgnoreProperties注释,如:

@JsonIgnoreProperties("products")
public ListingGroup listingGroup;

or like this:

或者像这样:

@JsonIgnoreProperties("listingGroup")
Set<Product> products;

or both.

或两者兼而有之。