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 ...
随机推荐
- my97datepicker插件日期值改变事件 等同于input的onchang()时间
官网Demo地址http://www.my97.net/demo/index.htm <input type="text" class="Wdate" v ...
- 基本Sql语句汇总
关于Sql语句的学习,选择的DBMS为SQL Server,Sql语句随着工作中的应用不断补充,不具备系统性,为个人笔记汇总,网上有很多优秀的资源,故不对每一处应用做过多细致的说明,后期会对部分篇幅较 ...
- netcore使用AutoMapper
说明:以下是使用过程中看到的其他博主写的,地址如下: 地址一:https://cloud.tencent.com/developer/article/1395155 地址二:https://www.c ...
- PyMySQL操作mysql数据库(py3必学)
一,安装PyMySQL Python是编程语言,MySQL是数据库,它们是两种不同的技术:要想使Python操作MySQL数据库需要使用驱动.这里选用PyMySQL驱动. 安装方式还是使用pip命令. ...
- CodeForces 632A
A - Grandma Laura and Apples Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- SpringBoot Beans定义 连接池
SpringBoot Beans定义 原有Spring框架,定义Bean方法如下 xml配置 组件扫描.@Controller.@Service... 原有Spring框架,参数注入方法如下 常用的参 ...
- EsAlert
https://www.cnblogs.com/zhaishaomin/p/7417306.html https://blog.csdn.net/pujiaolin/article/details/5 ...
- Segments--poj3304(判断直线与线段之间的关系)
http://poj.org/problem?id=3304 给你几条线段 然后 让你找到一条直线让他在这条直线上的映射有一个重合点 如果有这条直线的话 这个重合的部分的两个端点一定是某两条线段的 ...
- python 爬虫(转,我使用的python3)
原文地址:http://blog.csdn.net/pi9nc/article/details/9734437 [Python]网络爬虫(一):抓取网页的含义和URL基本构成 分类: 爬虫 Pyt ...
- hihocoder 1165 : 益智游戏
时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 幽香今天心情不错,正在和花田里的虫子玩一个益智游戏.这个游戏是这样的,对于一个数组A,幽香从A中选择一个数a,虫子从A中选 ...