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
jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore的更多相关文章
- jackson中的@JsonBackReference
# StackOverflowError / 无限递归 / json递归 / JsonBackReference 环境:springmvc+hibernate+json 在controller返 ...
- jackson中@JsonProperty、@JsonIgnore等常用注解总结
本文为博主原创,未经允许不得转载: 最近用的比较多,把json相关的知识点都总结一下,jackjson的注解使用比较频繁, jackson的maven依赖 <dependency> < ...
- jackson中JSON字符串节点遍历和修改
有些场景下,在实现一些基础服务和拦截器的时候,我们可能需要在不知道JSON字符串所属对象类型的情况下,对JSON字符串中的某些属性进行遍历和修改,比如,设置或查询一些报文头字段. 在jackson中, ...
- 关于Jackson中JsonNode的取值asText()和textValue()区别
在 比较高版本的Jackson 中, 包名为 com.fasterxml.jackson String jsonText="{\"name\":\"张三\&qu ...
- Jackson中的那些坑
不符合驼峰规范的变量 “驼峰命名法”请自行百度.简单的来说就是变量的第一个单词以小写字母开始其他单词首字母大写,或者全部单词首字母都大写,分别称为“小驼峰”和“大驼峰” 比如一个符合驼峰规范命名的实体 ...
- Jackson中@JsonProperty等常用注解
Java生态圈中有很多处理JSON和XML格式化的类库,Jackson是其中比较著名的一个.虽然JDK自带了XML处理类库,但是相对来说比较低级 本文将介绍的Jackson常用注解:精简概述 Jack ...
- net.sf.json和 com.fasterxml.jackson中对象转json的区别
近期做项目的时候,发现使用net.sf.json包中的JSONObject或JSONArray将对象转为json数据结构存在一个坑.当对String类型的属性赋值为null情况下,转为json结构为& ...
- 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 ...
- 【java】jackson 中JsonFormat date类型字段的使用
为了便于date类型字段的序列化和反序列化,需要在数据结构的date类型的字段上用JsonFormat注解进行注解具体格式如下 @JsonFormat(pattern = "yyyy-MM- ...
随机推荐
- SQL —— 存储过程
一.什么是存储过程 预先存储好的SQL程序. 保存在SQL Server中(跟视图的存储方式一样) 通过名称和参数执行. 二.存储过程的优点 执行速度更快 允许模块化程序设计 提高系统安全性 减少网络 ...
- WCF ChannelFactory
public static class WcfExtensions{ public static void Using<T>(this T client, Action<T&g ...
- 【JZOJ3853】【NOIP2014八校联考第2场第2试9.28】帮助Bsny(help)
EVRT Bsny的书架乱成一团了,帮他一下吧! 他的书架上一共有n本书,我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30,30,31,31,32,那么混乱值为3:30,32,32,3 ...
- poj2987 最大权闭合图
基础题. 最小割后,与汇点相连的点都不要,然后从源点出发dfs一遍有多少相连的点即可. #include<stdio.h> #include<string.h> #includ ...
- 利用阿里大于实现发送短信(JAVA版)
本文是我自己的亲身实践得来,喜欢的朋 友别忘了点个赞哦! 最近整理了一下利用阿里大于短信平台来实现发送短信功能. 闲话不多说,直接开始吧. 首先,要明白利用大于发送短信这件事是由两部分组成: 一.在阿 ...
- 重磅!容器集群监控利器 阿里云Prometheus 正式免费公测
Prometheus 作为容器生态下集群监控的首选方案,是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,并于 2015 年正式发布.2016 年,Prometheu ...
- Servlet身份验证过滤器
文件:index.html 文件:MyFilter.java 文件:MyServlet.java 文件:web.xml
- 设备 VMnet0 上的网络桥接当前未在运行。
早上,我打开我的虚拟机,却发现一个问题, 桥接网络怎么都连接不上. 报的是如下的错误 ------------------------------ 设备 VMnet0 上的网络桥接当前未在运行.该虚拟 ...
- 通过反射 往泛型Integer的集合里添加String 类型的数据 Day25
package com.sxt.method1; import java.lang.reflect.Method; /* * 需求:通过反射 往泛型Integer的集合里添加String 类型的数据 ...
- Person Re-identification 系列论文笔记(一):Scalable Person Re-identification: A Benchmark
打算整理一个关于Person Re-identification的系列论文笔记,主要记录近年CNN快速发展中的部分有亮点和借鉴意义的论文. 论文笔记流程采用contributions->algo ...