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.

  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/

时间: 2024-10-24 13:50:09

JSON: Circular Dependency Errors的相关文章

在.NET Core中遭遇循环依赖问题&quot;A circular dependency was detected&quot;

今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of type 'CNBlogs.Application.Interfaces.ITagService' 一开始以为是项目之间的引用关系引起的,在project.json中找来找去,一无所获. 后来从构造函数下手,才发现问题所在. 实现ITagService的类TagService的构造函数是这么定义的:

Circular dependency issuse on cocoapods version(0.36.0) 完全解决方案(非降版本)

此前的文章中,曾经提到在cocoapods依赖管理版本如果超过0.34.4,比如0.35, 0.36.0等版本中.执行"pod install" 或者 "pod update" , xmppframework 会出现循环依赖问题. There is a circular dependency between XMPPFramework/Core and XMPPFramework/Authentication' 解决问题的临时方案,是降版本 cocoapods 到

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

此前的文章中.以前提到在cocoapods依赖管理版本号假设超过0.34.4.比方0.35, 0.36.0等版本号中. 运行"pod install" 或者 "pod update" , xmppframework 会出现循环依赖问题. There is a circular dependency between XMPPFramework/Core and XMPPFramework/Authentication' 解决这个问题的暂时方案,是降版本号 cocoap

Intellij Error:Cannot build Artifact &#39;XXX:war exploded&#39; because it is included into a circular dependency

https://stackoverflow.com/questions/42335718/intellij-errorcannot-build-artifact-xxxwar-exploded-because-it-is-included-i 基本按这个来就好了 如果到了build artfact哪里按钮是灰色 就要手动建了 https://jingyan.baidu.com/album/0a52e3f43d9f69bf62ed72f9.html?picindex=8 这里的百度经验真是久违的不

Error:Cannot build artifact &#39;XXX:war exploded&#39; because it is included into a circular dependency (artifact &#39;XXXX:war exploded&#39;, artifact &#39;XXX:war exploded&#39;) Idea启动项目报错解决方案

在Idea中使用Maven创建父子工程,第一个Model的那个项目可以很好的运行,在创建一个Model运行时报这个错.原因是tomcat部署了多个Web项目,可能最开始是两个项目的配置文件混用用,最后就报这个错. 网上找的解决方案: 步骤:File--  Porject Struccture ---- Artifacts 在Tomcat部署时war和war exploded区别: war模式:将WEB工程以包的形式上传到服务器 : war exploded模式:将WEB工程以当前文件夹的位置关系

JSON与Javabean转换的几种形式

JSON格式的数据传递是最常用的方法之一,以下列出了常用的几种形态以及与Javabean之间的转换: String json1="{'name':'zhangsan','age':23,'interests':[{'interest':'篮球','colors':['绿色','黄色']},{'interest':'足球','colors':['红色','蓝色']}]}"; String json2="[{'name':'zhangsan'},{'name':'lisi'},{

SpringBoot 返回json 字符串(jackson 及 fast json)

一.jackson 1.Controller 类加注解@RestController 这个注解相当于@Controller 这个注解加 @ResponseBody 2.springBoot 默认使用 jackson 来把java 对象转化为json 字符串. 二.fast json 1.pom 文件加入fast json 依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson&l

(转)Struts2返回JSON对象的方法总结

转自:http://kingxss.iteye.com/blog/1622455 如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择.而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好的支持.但是,在开发工作中,对功能的升级是基于既定架构是很常见的情况.本人碰到需要用开发基于Struts2的HTTP+JSON返回类型接口就是基于既定框架结构下进行的. Struts2返回JSON有两种方式:1.使用S

spring Mvc json返回json的日期格式问题

(一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加jackson-mapper-asl <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version