当前位置:网站首页>@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
2022-07-06 08:30:00 【XYLANCC】
jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归(infinite recursion)问题。这些标注均可用在属性或对应的get、set方法中。
@JsonBackReference和@JsonManagedReference:这两个标注通常配对使用,通常用在父子关系中。@JsonBackReference标注的属性在序列化(serialization,即将对象转换为json数据)时,会被忽略(即结果中的json数据不包含该属性的内容)。@JsonManagedReference标注的属性则会被序列化。在序列化时,@JsonBackReference的作用相当于@JsonIgnore,此时可以没有@JsonManagedReference。但在反序列化(deserialization,即json数据转换为对象)时,如果没有@JsonManagedReference,则不会自动注入@JsonBackReference标注的属性(被忽略的父或子);如果有@JsonManagedReference,则会自动注入自动注入@JsonBackReference标注的属性。
@JsonIgnore:直接忽略某个属性,以断开无限递归,序列化或反序列化均忽略。当然如果标注在get、set方法中,则可以分开控制,序列化对应的是get方法,反序列化对应的是set方法。在父子关系中,当反序列化时,@JsonIgnore不会自动注入被忽略的属性值(父或子),这是它跟@JsonBackReference和@JsonManagedReference最大的区别。
示例测试代码(注意反序列化后的TreeNode[readValue]的children里的parent):
TreeNode.java
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonManagedReference;
public class TreeNode {
String name;
@JsonBackReference
// @JsonIgnore
TreeNode parent;
@JsonManagedReference
List<TreeNode> children;
public TreeNode() {
}
public TreeNode(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public void addChild(TreeNode child) {
if (children == null)
children = new ArrayList<TreeNode>();
children.add(child);
}
}
JsonTest.java
Java代码 收藏代码
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class JsonTest {
static TreeNode node;
@BeforeClass
public static void setUp() {
TreeNode node1 = new TreeNode("node1");
TreeNode node2 = new TreeNode("node2");
TreeNode node3 = new TreeNode("node3");
TreeNode node4 = new TreeNode("node4");
TreeNode node5 = new TreeNode("node5");
TreeNode node6 = new TreeNode("node6");
node1.addChild(node2);
node2.setParent(node1);
node2.addChild(node3);
node3.setParent(node2);
node2.addChild(node4);
node4.setParent(node2);
node3.addChild(node5);
node5.setParent(node3);
node5.addChild(node6);
node6.setParent(node5);
node = node3;
}
@Test
public void test() throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(node);
System.out.println(json);
TreeNode readValue = mapper.readValue(json, TreeNode.class);
System.out.println(readValue.getName());
}
@AfterClass
public static void tearDown() {
node = null;
}
} 边栏推荐
- Image,cv2读取图片的numpy数组的转换和尺寸resize变化
- Is it safe to open an account in Zheshang futures?
- Configuring OSPF load sharing for Huawei devices
- Restore backup data on S3 compatible storage with tidb lightning
- 2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
- The mysqlbinlog command uses
- JS inheritance method
- JVM 快速入门
- IoT -- 解读物联网四层架构
- C language double pointer -- classic question type
猜你喜欢

【MySQL】日志

FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战

Restful API design specification

Synchronized solves problems caused by sharing

Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers

电脑清理,删除的系统文件

2022.02.13 - NC001. Reverse linked list

Sort according to a number in a string in a column of CSV file

Unified ordering background interface product description Chinese garbled
随机推荐
Function coritization
Leetcode question brushing (5.28) hash table
【MySQL】鎖
如何进行接口测试测?有哪些注意事项?保姆级解读
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
sublime text中conda环境中plt.show无法弹出显示图片的问题
【刷题】牛客网面试必刷TOP101
延迟初始化和密封类
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
marathon-envs项目环境配置(强化学习模仿参考动作)
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Sort according to a number in a string in a column of CSV file
torch建立的网络模型使用torchviz显示
软件卸载时遇到trying to use is on a network resource that is unavailable
电脑清理,删除的系统文件
China vanadium battery Market Research and future prospects report (2022 Edition)
JS native implementation shuttle box
Leetcode skimming (5.29) hash table
个人电脑好用必备软件(使用过)
Summary of MySQL index failure scenarios