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.

  1. @JsonManagedReference: This annotation should be put on the owner of the relationship, or as you may deem fit.
  2. @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.ObjectMapper
ObjectMapper 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的更多相关文章

  1. 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"

    今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...

  2. Circular dependency issuse on cocoapods version(0.36.0) 全然解决方式(非降版本号)

    此前的文章中.以前提到在cocoapods依赖管理版本号假设超过0.34.4.比方0.35, 0.36.0等版本号中. 运行"pod install" 或者 "pod u ...

  3. DAX的圈圈大坑:循环依赖关系错误circular dependency (单表篇)

    使用DAX中的某些函数特别类似Calculate这种函数创建计算列时很容易出现一种错误,叫做检测到循环依赖关系,即:A circular dependency was detected.对于刚接触Da ...

  4. 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 ...

  5. 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 ...

  6. 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项目,可能最开始是两个项目的配置文件混用用,最后 ...

  7. 在Windows平台下Qt的exe报错问题排查步骤

    在Windows平台下Qt的exe报错问题排查步骤 工具介绍: 1. Dependency Worker Dependency Worker是一个免费的用具用来扫描任何的32bit 或者64bit 的 ...

  8. Linux和windows 查看程序、进程的依赖库的方法

    Linux: 1. 利用ldd查看可执行程序的依赖库   [root@~]# ldd /usr/local/php/bin/php   linux-vdso.so.1 => (0x00007ff ...

  9. Some_tools

    Why and what There are lots of nice or great tools on the internet, sometimes I will just forget a p ...

随机推荐

  1. 02 requests模块

    requests模块 requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests会比urllib更加方便,可以节约我们大 ...

  2. Java学习关于集合框架的基础接口--Collection接口

     集合框架(Collection  Framework)是Java最强大的子系统之一,位于java.util 包中.集合框架是一个复杂的接口与和类层次,提供了管理对象组的最新技术.Java集合框架标准 ...

  3. python的cache修饰器

    简单的memory cache.可以用来内存缓存任意函数方法. #!/usr/bin/python import functools from threading import RLock impor ...

  4. 【Ajax 2】封装Ajax的核心对象:XMLHttpRequest对象

    导读:AJAX利用一个构建到所有现代浏览器内部的对象-XMLHttpRequest-来实现发送和接收HTTP请求与响应信息.那么,XMLHttpRequest对象是怎么创建和封装的呢? 一.简介 1. ...

  5. 谁才是最强战舰!-From 南京理工大学第八届程序设计大赛(校外镜像),博弈~~

    谁才是最强战舰! Time Limit: 1000MS Memory Limit: 65536KB Description 依阿华来到镇守府的第一件事情,就是找大和solo!然而这并不是什么好消息,说 ...

  6. POJ1159:Palindrome【dp】

    题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]= ...

  7. 洛谷P2414 - [NOI2011]阿狸的打字机

    Portal Description 首先给出一个只包含小写字母和'B'.'P'的操作序列\(s_0(|s_0|\leq10^5)\).初始时我们有一个空串\(t\),依次按\(s_0\)的每一位进行 ...

  8. msp430项目编程10

    msp430中项目---电子密码锁 1.扫描键盘工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  9. Minimum Spanning Tree.prim/kruskal(并查集)

    开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...

  10. Educational Codeforces Round 50 (Rated for Div. 2)F. Relatively Prime Powers

    实际上就是求在[2,n]中,x != a^b的个数,那么实际上就是要求x=a^b的个数,然后用总数减掉就好了. 直接开方求和显然会有重复的数.容斥搞一下,但实际上是要用到莫比乌斯函数的,另外要注意减掉 ...