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

jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore的更多相关文章

  1. jackson中的@JsonBackReference

    #    StackOverflowError / 无限递归 / json递归 / JsonBackReference 环境:springmvc+hibernate+json 在controller返 ...

  2. jackson中@JsonProperty、@JsonIgnore等常用注解总结

    本文为博主原创,未经允许不得转载: 最近用的比较多,把json相关的知识点都总结一下,jackjson的注解使用比较频繁, jackson的maven依赖 <dependency> < ...

  3. jackson中JSON字符串节点遍历和修改

    有些场景下,在实现一些基础服务和拦截器的时候,我们可能需要在不知道JSON字符串所属对象类型的情况下,对JSON字符串中的某些属性进行遍历和修改,比如,设置或查询一些报文头字段. 在jackson中, ...

  4. 关于Jackson中JsonNode的取值asText()和textValue()区别

    在 比较高版本的Jackson 中, 包名为 com.fasterxml.jackson String jsonText="{\"name\":\"张三\&qu ...

  5. Jackson中的那些坑

    不符合驼峰规范的变量 “驼峰命名法”请自行百度.简单的来说就是变量的第一个单词以小写字母开始其他单词首字母大写,或者全部单词首字母都大写,分别称为“小驼峰”和“大驼峰” 比如一个符合驼峰规范命名的实体 ...

  6. Jackson中@JsonProperty等常用注解

    Java生态圈中有很多处理JSON和XML格式化的类库,Jackson是其中比较著名的一个.虽然JDK自带了XML处理类库,但是相对来说比较低级 本文将介绍的Jackson常用注解:精简概述 Jack ...

  7. net.sf.json和 com.fasterxml.jackson中对象转json的区别

    近期做项目的时候,发现使用net.sf.json包中的JSONObject或JSONArray将对象转为json数据结构存在一个坑.当对String类型的属性赋值为null情况下,转为json结构为& ...

  8. Jackson中处理map中的null key 或者null value 及实体字段中的null value

    1.map中有null key时的序列化  当有null key时,jackson序列化会报 Null key for a Map not allowed in JSON (use a convert ...

  9. 【java】jackson 中JsonFormat date类型字段的使用

    为了便于date类型字段的序列化和反序列化,需要在数据结构的date类型的字段上用JsonFormat注解进行注解具体格式如下 @JsonFormat(pattern = "yyyy-MM- ...

随机推荐

  1. 简单介绍几个CSSReset的方法

    对于小型的网站来说,用这个并不会带来大的资源浪费,但如果是像Yahoo这种架构非常大的网站,必须要有选择地进行CSS重设,以减 少资源浪费. 正在使用CSS的你,用过CSS Reset吗?当然,或许你 ...

  2. DIV+CSS网页布局常用的一些基础知识整理

    CSS命名规范 一.文件命名规范 全局样式:global.css: 框架布局:layout.css: 字体样式:font.css: 链接样式:link.css: 打印样式:print.css: 二.常 ...

  3. Nginx教程(7) 正向代理与反向代理【总结】 (转)

    1.前言 最近工作中用到反向代理,发现网络代理的玩法还真不少,网络背后有很多需要去学习.而在此之前仅仅使用了过代理软件,曾经为了访问google,使用了代理软件,需要在浏览器中配置代理的地址.我只知道 ...

  4. python 正确地初始化对象

  5. spring调试

    spring-beans DefaultListableBeanFactory preInstantiateSingletons:650 RequestMappingHandlerMapping Ab ...

  6. hdu2149 巴什博奕

    n表示先手胜 p表示先手负 1~N N+1 N+2~2*N+1 2*N+2 2*N+3~3*N+2 ... n p n p n ... m总归个数 每次取1~N个根据前面的规律得到,如果m/N==m% ...

  7. js、php判断手机PC

    用于phpcms <script type="text/javascript"> var url = window.location.href; if(/Android ...

  8. Laravel 发送邮件(最简单的讲解!)

    Laravel集成了SwiftMailer库进行邮件发送,邮件配置文件位于config/mail.php:. return [ 'driver' => env('MAIL_DRIVER', 's ...

  9. 三角形数且是完全平方数 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E.Half-consecutive Numbers

    三角形数:an=n*(n+1)/2; 完全平方数:bn=c^2; 既是三角形数又是完全平方数:An=6*A(n-1)-A(n-2)+2; A[23]={ 0, 1, 8, 49, 288, 1681, ...

  10. lavarel box 地址

    https://atlas.hashicorp.com/laravel/boxes/homestead download URL https://atlas.hashicorp.com/laravel ...