jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore
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
- 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;
- }
- }
原文地址:https://blog.csdn.net/qq_35357001/article/details/55505659
最新文章
- 开发常用之在webstorm中使用cmd
- bzoj2946: [Poi2000]公共串
- Python 基础【第九篇】运算
- Ubuntu 源
- python第一次上机遇到的困难
- TPYBoard v102的GPIO使用用法
- read运行
- org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreatio
- IDEA打印gc日志,设置JVM参数方法
- 深度学习框架PyTorch一书的学习-第五章-常用工具模块
- ORA-03113: end-of-file on communication channel 磁盘慢,数据库启动失败
- C/C++ 中嵌入 arm 汇编
- Scrapy-Splash的介绍、安装以及实例
- P2157 [SDOI2009]学校食堂
- js格式化input输入框内容(每几位分一组,并使用特定字符分隔)
- zoj3469 区间dp好题
- 虚拟路由冗余协议VRRP
- CCF 201409-4 最优配餐
- liunx系统安装jdk的方法
- Elasticsearch增、删、改、查操作深入详解
热门文章
- C++学习笔记(1)-构造函数与析构函数
- springboot整合neo4j
- php 如何生成静态页
- cocos2dX 之CCAnimation/CCAnimate
- Dubbo报org.I0Itec.zkclient.exception.ZkNoNodeException异常
- js日历
- python 类的创建
- C++ 引用#include<;math.h>; 找不到动态库
- qbao
- 解决PowerDesigner中DBMS选项卡为空白