Jackson-deserialization fails on circular dependencies(JackSon无限递归问题)
<div class="row">
<div class="col">
Ok, so I'm trying to test some stuffs with jackson json converter.
I'm trying to simulate a graph behaviour, so these are my POJO entities
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ParentEntity implements java.io.Serializable
{
private String id;
private String description;
private ParentEntity parentEntity;
private List<ParentEntity> parentEntities = new ArrayList<ParentEntity>(0);
private List<ChildEntity> children = new ArrayList<ChildEntity>(0);
// ... getters and setters
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ChildEntity implements java.io.Serializable
{
private String id;
private String description;
private ParentEntity parent;
// ... getters and setters
}
The tags are required in order to avoid exception on serialization.
When I try to serialize an object (both on a file or on a simple string) all works fine. However, when I try to deserialize the object, it throws an exception. This is the simple test method (try/catch omitted for simplicity)
{
// create some entities, assigning them some values
ParentEntity pe = new ParentEntity();
pe.setId("1");
pe.setDescription("first parent");
</span><span class="typ">ChildEntity</span><span class="pln"> ce1 </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span class="hljs-keyword">new</span></span><span class="pln"> </span><span class="typ">ChildEntity</span><span class="pun">();</span><span class="pln">
ce1</span><span class="pun">.</span><span class="pln">setId</span><span class="pun">(</span><span class="str"><span class="hljs-string">"1"</span></span><span class="pun">);</span><span class="pln">
ce1</span><span class="pun">.</span><span class="pln">setDescription</span><span class="pun">(</span><span class="str"><span class="hljs-string">"first child"</span></span><span class="pun">);</span><span class="pln">
ce1</span><span class="pun">.</span><span class="pln">setParent</span><span class="pun">(</span><span class="pln">pe</span><span class="pun">);</span><span class="pln">
</span><span class="typ">ChildEntity</span><span class="pln"> ce2 </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span class="hljs-keyword">new</span></span><span class="pln"> </span><span class="typ">ChildEntity</span><span class="pun">();</span><span class="pln">
ce2</span><span class="pun">.</span><span class="pln">setId</span><span class="pun">(</span><span class="str"><span class="hljs-string">"2"</span></span><span class="pun">);</span><span class="pln">
ce2</span><span class="pun">.</span><span class="pln">setDescription</span><span class="pun">(</span><span class="str"><span class="hljs-string">"second child"</span></span><span class="pun">);</span><span class="pln">
ce2</span><span class="pun">.</span><span class="pln">setParent</span><span class="pun">(</span><span class="pln">pe</span><span class="pun">);</span><span class="pln">
pe</span><span class="pun">.</span><span class="pln">getChildren</span><span class="pun">().</span><span class="pln">add</span><span class="pun">(</span><span class="pln">ce1</span><span class="pun">);</span><span class="pln">
pe</span><span class="pun">.</span><span class="pln">getChildren</span><span class="pun">().</span><span class="pln">add</span><span class="pun">(</span><span class="pln">ce2</span><span class="pun">);</span><span class="pln">
</span><span class="typ">ParentEntity</span><span class="pln"> pe2 </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span class="hljs-keyword">new</span></span><span class="pln"> </span><span class="typ">ParentEntity</span><span class="pun">();</span><span class="pln">
pe2</span><span class="pun">.</span><span class="pln">setId</span><span class="pun">(</span><span class="str"><span class="hljs-string">"2"</span></span><span class="pun">);</span><span class="pln">
pe2</span><span class="pun">.</span><span class="pln">setDescription</span><span class="pun">(</span><span class="str"><span class="hljs-string">"second parent"</span></span><span class="pun">);</span><span class="pln">
pe2</span><span class="pun">.</span><span class="pln">setParentEntity</span><span class="pun">(</span><span class="pln">pe</span><span class="pun">);</span><span class="pln">
pe</span><span class="pun">.</span><span class="pln">getParentEntities</span><span class="pun">().</span><span class="pln">add</span><span class="pun">(</span><span class="pln">pe2</span><span class="pun">);</span><span class="pln">
</span><span class="com"><span class="hljs-comment">// serialization</span></span><span class="pln">
</span><span class="typ">ObjectMapper</span><span class="pln"> mapper </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span class="hljs-keyword">new</span></span><span class="pln"> </span><span class="typ">ObjectMapper</span><span class="pun">();</span><span class="pln">
</span><span class="typ">File</span><span class="pln"> f </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span class="hljs-keyword">new</span></span><span class="pln"> </span><span class="typ">File</span><span class="pun">(</span><span class="str"><span class="hljs-string">"parent_entity.json"</span></span><span class="pun">);</span><span class="pln">
</span><span class="com"><span class="hljs-comment">// write to file</span></span><span class="pln">
mapper</span><span class="pun">.</span><span class="pln">writeValue</span><span class="pun">(</span><span class="pln">f</span><span class="pun">,</span><span class="pln"> pe</span><span class="pun">);</span><span class="pln">
</span><span class="com"><span class="hljs-comment">// write to string</span></span><span class="pln">
</span><span class="typ">String</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> mapper</span><span class="pun">.</span><span class="pln">writeValueAsString</span><span class="pun">(</span><span class="pln">pe</span><span class="pun">);</span><span class="pln">
</span><span class="com"><span class="hljs-comment">// deserialization</span></span><span class="pln">
</span><span class="com"><span class="hljs-comment">// read from file</span></span><span class="pln">
</span><span class="typ">ParentEntity</span><span class="pln"> pe3 </span><span class="pun">=</span><span class="pln"> mapper</span><span class="pun">.</span><span class="pln">readValue</span><span class="pun">(</span><span class="pln">f</span><span class="pun">,</span><span class="typ">ParentEntity</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">);</span><span class="pln">
</span><span class="com"><span class="hljs-comment">// read from string</span></span><span class="pln">
</span><span class="typ">ParentEntity</span><span class="pln"> pe4 </span><span class="pun">=</span><span class="pln"> mapper</span><span class="pun">.</span><span class="pln">readValue</span><span class="pun">(</span><span class="pln">s</span><span class="pun">,</span><span class="pln"> </span><span class="typ">ParentEntity</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">);</span><span class="pln">
}
and this is the exception thrown (of course, repeated twice)
com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.String) [com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey@3372bb3f] (through reference chain: ParentEntity["children"]->java.util.ArrayList[0]->ChildEntity["id"])
...stacktrace...
Caused by: java.lang.IllegalStateException: Already had POJO for id (java.lang.String) [com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey@3372bb3f]
...stacktrace...
So, what is the cause of the problem? How can I fix it? Do I need some other annotation?
Jackson-deserialization fails on circular dependencies(JackSon无限递归问题)的更多相关文章
- jackson java转json hibernate懒加载造成的无限递归问题
@JsonIgnore @JsonFilter @JsonBackReference @JsonManagedReference @JsonIgnoreProperties jackson中的@Jso ...
- JPA一对多循环引用的解决&&JackSon无限递归问题
说是解决,其实不是很完美的解决的,写出来只是想记录一下这个问题或者看一下有没有哪位仁兄会的,能否知道一二. 下面说说出现问题: 问题是这样的,当我查询一个一对多的实体的时候,工具直接就爆了,差不多我就 ...
- 开发问题及解决--java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android=" ...
- java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- hibernate无限递归问题
项目异常如下: 2018-01-26 17:12:38.162 WARN 3128 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionReso ...
- 无限递归的构造器和javap使用指南
无限递归的构造器和javap使用指南 public class ConstructorRecursion { ConstructorRecursion rc; { rc = newConstructo ...
- 无限“递归”的python程序
如果一个函数直接或者间接调用了自己,那么就形成了递归(recursion),比如斐波那契数列的一个实现 def fib(n): if n <= 2: return 1 else: return ...
- 【整理】iview Tree数据格式问题,无限递归树处理数据
iview Tree数据格式问题,无限递归树处理数据 https://juejin.im/post/5b51a8a4e51d455d6825be20
- -java转json hibernate懒加载造成的无限递归问题
1.在判断到底是谁维护关联关系时,可以通过查看外键,哪个实体类定义了外键,哪个类就负责维护关联关系. JoinColumn(name="pid") 2. 在保存数据时,总是先保存的 ...
随机推荐
- Python数据分析与展示[第一周]
ipython 中的问号 获得相关的描述信息 %run 系统文件 执行某一个文件 ipython的模式命令 %magic 显示所有的魔术命令 %hist 命令历史输入信息 %pdb 异常发 ...
- JS 鼠标键盘HTML事件
- JVM学习篇章(一)
熟悉Java开发的同学,应该都知道jvm是什么,---它就是Java虚拟机,今天我们就学习一下: 内存分配原则: 常用的监控命令
- SQLServer —— 数据类型的转换
一.使用convert函数实现强制转换 例如我们现在有如下一张学员成绩表: 现在想查询学号等于100003的学员总成绩,并按照要求打印出来,我们可以这样实现: 结果报错,因为最后一句字符串不能和数值相 ...
- left join table_a on ...like...
- iOS 微信支付如果遇到跳转只有一个确定请看这里
http://www.cocoachina.com/bbs/read.php?tid-321546.html 今天在联调微信支付,不得不说,和它比起来,阿里的支付sdk真的是太好用了.果然和后端同学在 ...
- @spoj - ADAMOLD@ Ada and Mold
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 N 的序列 A,将其划分成 K + 1 段,划分 ...
- Pycurl介绍
pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...
- [React Native]获取网络状态
使用React Native,可以使用NetInfo API获取手机当前的各个网络状态. componentWillMount() { NetInfo.fetch().done((status)=&g ...
- CSDN编程挑战——《-3+1》
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/user_longling/article/details/24674033 -3+1 题目详情: 有 ...