当前位置:网站首页>@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
2022-07-06 08:38:00 【XYLANCC】
jackson Medium @JsonBackReference and @JsonManagedReference, as well as @JsonIgnore It is to solve the infinite recursion caused by the existence of two-way references in the object (infinite recursion) problem . These dimensions can be used in attributes or corresponding get、set In the method .
@JsonBackReference and @JsonManagedReference: These two labels are usually used in pairs , Usually used in parent-child relationships [email protected] The attributes of the annotation are serialized (serialization, Convert the object to json data ) when , Will be ignored ( That is... In the result json The data does not contain the content of this attribute )[email protected] The attributes of the annotation are serialized . At serialization ,@JsonBackReference Is equivalent to @JsonIgnore, There can be no @JsonManagedReference. But in deserialization (deserialization, namely json Data to object ) when , without @JsonManagedReference, Will not automatically inject @JsonBackReference The properties of the annotation ( Neglected parent or child ); If there is @JsonManagedReference, Will be automatically injected @JsonBackReference The properties of the annotation .
@JsonIgnore: Ignore an attribute directly , To break infinite recursion , Serialization or deserialization are ignored . Of course, if marked in get、set In the method , Can be controlled separately , Serialization corresponds to get Method , Deserialization corresponds to set Method . In a father son relationship , When deserializing ,@JsonIgnore Ignored attribute values are not automatically injected ( Parent or child ), This is it with @JsonBackReference and @JsonManagedReference The biggest difference .
Sample test code ( Pay attention to the deserialized TreeNode[readValue] Of children Inside 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 Code Collection code
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;
}
}
边栏推荐
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- Online yaml to CSV tool
- ROS编译 调用第三方动态库(xxx.so)
- C language double pointer -- classic question type
- IoT -- 解读物联网四层架构
- 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
- PC easy to use essential software (used)
- marathon-envs项目环境配置(强化学习模仿参考动作)
- 【MySQL】鎖
- 2022.02.13 - NC004. Print number of loops
猜你喜欢
IOT -- interpreting the four tier architecture of the Internet of things
MySQL learning records 12jdbc operation transactions
JVM quick start
sublime text的编写程序时的Tab和空格缩进问题
2022.02.13 - NC001. Reverse linked list
2022.02.13 - 238. Maximum number of "balloons"
[brush questions] top101 must be brushed in the interview of niuke.com
【刷题】牛客网面试必刷TOP101
生成器参数传入参数
tree树的精准查询
随机推荐
JVM performance tuning and practical basic theory - Part 1
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
logback1.3. X configuration details and Practice
ROS编译 调用第三方动态库(xxx.so)
2022.02.13 - NC001. Reverse linked list
String to leading 0
Synchronized solves problems caused by sharing
Restful API design specification
Detailed explanation of heap sorting
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
LDAP应用篇(4)Jenkins接入
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
【ROS】usb_cam相机标定
生成器参数传入参数
Crash problem of Chrome browser
Visual implementation and inspection of visdom
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
Ruffian Heng embedded bimonthly, issue 49
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