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  

Java代码   
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.codehaus.jackson.annotate.JsonBackReference;
  4. import org.codehaus.jackson.annotate.JsonManagedReference;
  5. public class TreeNode {
  6. String name;
  7. @JsonBackReference
  8. //  @JsonIgnore
  9. TreeNode parent;
  10. @JsonManagedReference
  11. List<TreeNode> children;
  12. public TreeNode() {
  13. }
  14. public TreeNode(String name) {
  15. this.name = name;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public TreeNode getParent() {
  24. return parent;
  25. }
  26. public void setParent(TreeNode parent) {
  27. this.parent = parent;
  28. }
  29. public List<TreeNode> getChildren() {
  30. return children;
  31. }
  32. public void setChildren(List<TreeNode> children) {
  33. this.children = children;
  34. }
  35. public void addChild(TreeNode child) {
  36. if (children == null)
  37. children = new ArrayList<TreeNode>();
  38. children.add(child);
  39. }
  40. }

JsonTest.java  

Java代码   
  1. import java.io.IOException;
  2. import org.codehaus.jackson.JsonGenerationException;
  3. import org.codehaus.jackson.map.JsonMappingException;
  4. import org.codehaus.jackson.map.ObjectMapper;
  5. import org.junit.AfterClass;
  6. import org.junit.BeforeClass;
  7. import org.junit.Test;
  8. public class JsonTest {
  9. static TreeNode node;
  10. @BeforeClass
  11. public static void setUp() {
  12. TreeNode node1 = new TreeNode("node1");
  13. TreeNode node2 = new TreeNode("node2");
  14. TreeNode node3 = new TreeNode("node3");
  15. TreeNode node4 = new TreeNode("node4");
  16. TreeNode node5 = new TreeNode("node5");
  17. TreeNode node6 = new TreeNode("node6");
  18. node1.addChild(node2);
  19. node2.setParent(node1);
  20. node2.addChild(node3);
  21. node3.setParent(node2);
  22. node2.addChild(node4);
  23. node4.setParent(node2);
  24. node3.addChild(node5);
  25. node5.setParent(node3);
  26. node5.addChild(node6);
  27. node6.setParent(node5);
  28. node = node3;
  29. }
  30. @Test
  31. public void test() throws JsonGenerationException, JsonMappingException, IOException {
  32. ObjectMapper mapper = new ObjectMapper();
  33. String json = mapper.writeValueAsString(node);
  34. System.out.println(json);
  35. TreeNode readValue = mapper.readValue(json, TreeNode.class);
  36. System.out.println(readValue.getName());
  37. }
  38. @AfterClass
  39. public static void tearDown() {
  40. node = null;
  41. }
  42. }

原文地址:https://blog.csdn.net/qq_35357001/article/details/55505659

最新文章

  1. 开发常用之在webstorm中使用cmd
  2. bzoj2946: [Poi2000]公共串
  3. Python 基础【第九篇】运算
  4. Ubuntu 源
  5. python第一次上机遇到的困难
  6. TPYBoard v102的GPIO使用用法
  7. read运行
  8. org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreatio
  9. IDEA打印gc日志,设置JVM参数方法
  10. 深度学习框架PyTorch一书的学习-第五章-常用工具模块
  11. ORA-03113: end-of-file on communication channel 磁盘慢,数据库启动失败
  12. C/C++ 中嵌入 arm 汇编
  13. Scrapy-Splash的介绍、安装以及实例
  14. P2157 [SDOI2009]学校食堂
  15. js格式化input输入框内容(每几位分一组,并使用特定字符分隔)
  16. zoj3469 区间dp好题
  17. 虚拟路由冗余协议VRRP
  18. CCF 201409-4 最优配餐
  19. liunx系统安装jdk的方法
  20. Elasticsearch增、删、改、查操作深入详解

热门文章

  1. C++学习笔记(1)-构造函数与析构函数
  2. springboot整合neo4j
  3. php 如何生成静态页
  4. cocos2dX 之CCAnimation/CCAnimate
  5. Dubbo报org.I0Itec.zkclient.exception.ZkNoNodeException异常
  6. js日历
  7. python 类的创建
  8. C++ 引用#include&lt;math.h&gt; 找不到动态库
  9. qbao
  10. 解决PowerDesigner中DBMS选项卡为空白