JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directional mappings then you can be sure of getting the following errors if you try to generate JSON of the entities involved.
1. If you’re using Jackson
|
1
|
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) |
2. If you’re using Google’s Gson
|
1
|
java.lang.IllegalStateException: circular reference error |
Let’s take a case of two entities Actor, and Movie having a bi-directional relationship and using them we’ll see how to get over the above exceptions when we try to generate JSON.
This solution uses Jackson for generating JSON.
So if you’re using Google’s Gson you’ll have to write an exclusion strategy yourself, and more importantly the rest of the post is useless for you, unless you’re willing to switch to Jackson like I did with such great finesse! ;)
If yes, then you can download Jackson’s libraries and continue reading the post further, rest assured that it’ll be of immense help to you! :)
Now, these entities will have a many-to-many relationship amongst them. As one Actor can act in many Movies and one Movie can have many Actors (as it so often happens, and by the way due apologies for being trite and rhetoric!).
Actor.java
|
1
2
3
4
5
6
7
8
9
10
|
@Entity@Table(name="Actor")public class Actor implements Serializable { . . . /* This is the exception-saving-annotation */ @JsonManagedReference @ManyToMany private List<Movie> movies; . . .} |
Movie.java
|
1
2
3
4
5
6
7
8
9
10
|
@Entity@Table(name="Movie")public class Movie implements Serializable { . . . /* This is the exception-saving-annotation */ @JsonBackReference @ManyToMany(mappedBy="movies") private List<Actor> actors; . . .} |
So all we did was adding two annotations to the fields having a relationship.
- @JsonManagedReference: This annotation should be put on the owner of the relationship, or as you may deem fit.
- @JsonBackReference: This annotation should be used on the other end of the relationship.
Now, having done that it’s extremely easy to generate the JSON.
|
1
2
3
4
5
6
|
//com.fasterxml.jackson.databind.ObjectMapperObjectMapper objectMapper = new ObjectMapper();//The input argument of the writeValueAsString() function can be a bean, array, list, map or a set.String actorsAsJson = objectMapper.writeValueAsString(actorsList);//actorsList is a variable of type List<Actor>//Voila! You're done! |
That’s all for the circular dependency errors.
source :https://ankeetmaini.wordpress.com/2012/07/26/json-troubleshooting-circular-dependency-errors/
JSON: Circular Dependency Errors的更多相关文章
- 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...
- Circular dependency issuse on cocoapods version(0.36.0) 全然解决方式(非降版本号)
此前的文章中.以前提到在cocoapods依赖管理版本号假设超过0.34.4.比方0.35, 0.36.0等版本号中. 运行"pod install" 或者 "pod u ...
- DAX的圈圈大坑:循环依赖关系错误circular dependency (单表篇)
使用DAX中的某些函数特别类似Calculate这种函数创建计算列时很容易出现一种错误,叫做检测到循环依赖关系,即:A circular dependency was detected.对于刚接触Da ...
- Intellij Error:Cannot build Artifact 'XXX:war exploded' because it is included into a circular dependency
外网的流程是这样的. 1: 2: 3: 4: 基本按这个来就好了 如果到了build artfact哪里按钮是灰色 就要手动建了 https://jingyan.baidu.com/album/0a5 ...
- Error:Cannot build artifact 'ssm:war exploded' because it is included into a circular dependency (artifact 'ssm:war exploded', artifact 'apinb-master:war exploded')
打开 File->Project Structure –> Artifacts(ctrl+alt+shift+s) ,这里会有4个,我已经删除了,把 ssm:war 和 ssm:war e ...
- Error:Cannot build artifact 'XXX:war exploded' because it is included into a circular dependency (artifact 'XXXX:war exploded', artifact 'XXX:war exploded') Idea启动项目报错解决方案
在Idea中使用Maven创建父子工程,第一个Model的那个项目可以很好的运行,在创建一个Model运行时报这个错.原因是tomcat部署了多个Web项目,可能最开始是两个项目的配置文件混用用,最后 ...
- 在Windows平台下Qt的exe报错问题排查步骤
在Windows平台下Qt的exe报错问题排查步骤 工具介绍: 1. Dependency Worker Dependency Worker是一个免费的用具用来扫描任何的32bit 或者64bit 的 ...
- Linux和windows 查看程序、进程的依赖库的方法
Linux: 1. 利用ldd查看可执行程序的依赖库 [root@~]# ldd /usr/local/php/bin/php linux-vdso.so.1 => (0x00007ff ...
- Some_tools
Why and what There are lots of nice or great tools on the internet, sometimes I will just forget a p ...
随机推荐
- Jmeter接口测试实战-Cookies
场景: 接口测试时常都需要登录,请求方式(post), 登录常用的方法有通过获取token, 获取session, 获取cookie, 等等. 这几种都有一个共同的特点, 有效期(expires). ...
- python025 Python3 正则表达式
Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...
- Leetcode 239.滑动窗口最大值
滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值. 示例: ...
- HDU3785寻找大富翁~~真真切切的水题
寻找大富翁 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- POJ-3100-Root of the Problem,原来是水题,暴力求解~~~
Root of the Problem Time Limit: 1000MS Memory Limit: 65536K http://poj.org/problem?i ...
- PTA 03-树3 Tree Traversals Again (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again (25分) An inor ...
- 【转】关于大型网站技术演进的思考(十九)--网站静态化处理—web前端优化—上(11)
网站静态化处理这个系列马上就要结束了,今天我要讲讲本系列最后一个重要的主题web前端优化.在开始谈论本主题之前,我想问大家一个问题,网站静态化处理技术到底是应该归属于web服务端的技术范畴还是应该归属 ...
- SPOJ FAVDICE 数学期望
题目大意: 一个有n面的色子抛掷多少次能使所有面都能被抛到过,求期望值 总面数为n,当已经抛到过 i 个不同面时,我们抛出下一个不同面的概率为 (n-i)/n,那么抛的次数为 n/(n-i) 将所有抛 ...
- [USACO11NOV]牛的障碍Cow Steeplechase(匈牙利算法)
洛谷传送门 题目描述: 给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段. 因为横的与横的,竖的 ...
- POJ 2553 The Bottom of a Graph 【scc tarjan】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...