📜  spring boot jackson 无限递归 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:42.385000             🧑  作者: Mango

代码示例1
// some solutions:

public class Product {
    @NotNull
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", nullable = false)
    @JsonBackReference
    private Category category;
}

public class Category {
    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category")
    @JsonManagedReference
    private List products  = new ArrayList<>();
}

// ______________________________________________

@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class, 
  property = "id")
public class Product {
    ...
}

@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class, 
  property = "id")
public class Category {
    ...
}

//________________________

// Just use @JsonIgnore on one of the sides