我正面临一个问题..我在春季启动时与jpa有多对多的关系,但我需要暴露以下内容 产品有很多标签,标签有很多产品
if query product / 1
{product:{name:"product 1"}, tags:[ tag1:{name:"tag 1"}, tag2:{name:"tag2"} ] }
if query tag / 1
{tag:1, products:[ product1:[{name:"product 1"}, tag2:{product:"tag2"} ] }
用弹簧靴启动休息的方法是什么? 一个例子,网址或想法它会有用。
答案 0 :(得分:2)
当您尝试序列化JPA bean时,需要使用@JsonManagedReference
和@JsonBackReference
注释的组合来阻止无限递归。
有关详细信息,请查看其中一些问题:
答案 1 :(得分:1)
有multiple alternatives to stop infinite recursion:
@JsonManagedReference
和@JsonBackReference
:@Entity
public class Product {
@ManyToMany
@JsonManagedReference
private List<Tag> tags = new ArrayList<Tag>();
}
@Entity
public class Tag implements {
@ManyToMany(mappedBy = "tags")
@JsonBackReference
private List<Product> products = new ArrayList<Product>();
}
@JsonIdentityInfo
:@Entity
public class Product {
@ManyToMany
private List<Tag> tags = new ArrayList<Tag>();
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity
public class Tag implements {
@ManyToMany(mappedBy = "tags")
private List<Product> products = new ArrayList<Product>();
}
@JsonIgnoreProperties
:@Entity
public class Product {
@ManyToMany
@JsonIgnoreProperties("products")
private List<Tag> tags = new ArrayList<Tag>();
}
@Entity
public class Tag implements {
@ManyToMany(mappedBy = "tags")
@JsonIgnoreProperties("tags")
private List<Product> products = new ArrayList<Product>();
}