JsonManagedReference 与 JsonBackReference
JsonManagedReference vs JsonBackReference
我想知道杰克逊@JsonManagedReference
和@JsonBackReference
的区别?
@JsonManagedReference is the forward part of reference – the one that
gets serialized normally. @JsonBackReference is the back part of
reference – it will be omitted from serialization.
所以他们真的取决于你们关系的发展方向
public class User {
public int id;
public String name;
@JsonBackReference
public List<Item> userItems;
}
public class Item {
public int id;
public String itemName;
@JsonManagedReference
public User owner;
}
@JsonManagedReference
和 @JsonBackReference
旨在处理字段之间的这种双向链接,一个用于父角色,另一个用于子角色。
For avoiding the problem, linkage is handled such that the property
annotated with @JsonManagedReference annotation is handled normally
(serialized normally, no special handling for deserialization) and the
property annotated with @JsonBackReference annotation is not
serialized; and during deserialization, its value is set to instance
that has the "managed" (forward) link.
- @JsonManagedReference -> 管理引用的前向部分,此注释标记的字段是被序列化的字段
- @JsonBackReference -> 管理引用的反向部分,标有此注解的fields/collections不序列化。
用例:
您的 entities/tables 中有一对多或多对多关系,不使用上述关系会导致
之类的错误
Infinite Recursion and hence Whosebug - > Could not write content: Infinite recursion (WhosebugError)
出现上述错误是因为 Jackson(或其他类似的人)试图序列化关系的两端并以递归结束。
@JsonIgnore 执行类似的功能,但上面提到的注释更可取。
我比较喜欢
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
其中 属性 是主键字段的名称,范围是它的类型
正如 Rajat Verma 所写,他的解决方案非常有效。谢谢你为我节省了很多时间和愤怒:-)
The important Part:
You need define fields as List
, I had that as Set
before and this solution NOT WORKING (appears as infinite loop)!
我添加我的解决方案:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Agent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(mappedBy = "subscribers")
@ApiModelProperty(dataType = "List", example = "[1,2,3]") // for Swagger
@JsonIdentityReference(alwaysAsId = true) // show only id of Topic
private final List<Topic> subscribeTopics = new ArrayList<>()
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Topic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "topic_agent",
joinColumns = @JoinColumn(name = "fk_topic_id"),
inverseJoinColumns = @JoinColumn(name = "fk_agent_id"))
@ApiModelProperty(dataType = "List", example = "[1,2,3]")
@JsonIdentityReference(alwaysAsId = true)
private final List<Agent> subscribers = new ArrayList<>();
}
我想知道杰克逊@JsonManagedReference
和@JsonBackReference
的区别?
@JsonManagedReference is the forward part of reference – the one that gets serialized normally. @JsonBackReference is the back part of reference – it will be omitted from serialization.
所以他们真的取决于你们关系的发展方向
public class User {
public int id;
public String name;
@JsonBackReference
public List<Item> userItems;
}
public class Item {
public int id;
public String itemName;
@JsonManagedReference
public User owner;
}
@JsonManagedReference
和 @JsonBackReference
旨在处理字段之间的这种双向链接,一个用于父角色,另一个用于子角色。
For avoiding the problem, linkage is handled such that the property annotated with @JsonManagedReference annotation is handled normally (serialized normally, no special handling for deserialization) and the property annotated with @JsonBackReference annotation is not serialized; and during deserialization, its value is set to instance that has the "managed" (forward) link.
- @JsonManagedReference -> 管理引用的前向部分,此注释标记的字段是被序列化的字段
- @JsonBackReference -> 管理引用的反向部分,标有此注解的fields/collections不序列化。
用例: 您的 entities/tables 中有一对多或多对多关系,不使用上述关系会导致
之类的错误Infinite Recursion and hence Whosebug - > Could not write content: Infinite recursion (WhosebugError)
出现上述错误是因为 Jackson(或其他类似的人)试图序列化关系的两端并以递归结束。
@JsonIgnore 执行类似的功能,但上面提到的注释更可取。
我比较喜欢@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
其中 属性 是主键字段的名称,范围是它的类型
正如 Rajat Verma 所写,他的解决方案非常有效。谢谢你为我节省了很多时间和愤怒:-)
The important Part:
You need define fields asList
, I had that asSet
before and this solution NOT WORKING (appears as infinite loop)!
我添加我的解决方案:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Agent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(mappedBy = "subscribers")
@ApiModelProperty(dataType = "List", example = "[1,2,3]") // for Swagger
@JsonIdentityReference(alwaysAsId = true) // show only id of Topic
private final List<Topic> subscribeTopics = new ArrayList<>()
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Topic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "topic_agent",
joinColumns = @JoinColumn(name = "fk_topic_id"),
inverseJoinColumns = @JoinColumn(name = "fk_agent_id"))
@ApiModelProperty(dataType = "List", example = "[1,2,3]")
@JsonIdentityReference(alwaysAsId = true)
private final List<Agent> subscribers = new ArrayList<>();
}