json infinite recursion stackoverflowerror

  昨天在公司平台上进行开发时遇到的问题,研究了一天才弄明白,具体情况如下:

  在controller返回数据到统一json转换的时候,出现了json infinite recursion stackoverflowerror的错误,即json在将对象转换为json格式的数据的时候,出现了无限递归调用的情况。

具体的情况如下:

  A类中,有个属性:List<B> b, A与B的关系为 OneToMany;在B类中,有属性A a,引用到A中的字段id,并作为外键。hibernate查询结果正常,可以看到返回的A对象中,有b参数值,但在json转换的时候就出现了无限递归的情况。个人分析,应该是json在序列化A中的b属性的时候,找到了B类,然后序列化B类,而B类中有a属性,因此,为了序列化a属性,json又得去序列化A类,如此递归反复,造成该问题。

解决:

  在B类中a的getter setter方法上加注解@JsonBackReference,其实自己试过只在setter方法上加@JsonBackReference也够了。

参考文章:

http://my.oschina.net/u/943437/blog/145246

时间: 2024-08-05 05:25:04

json infinite recursion stackoverflowerror的相关文章

JPA出现recursion 死循环导致栈内存溢出问题 Could not write JSON: Infinite recursion (StackOverflowError)

@JsonIgnore 被注解的字段忽略被序列化, 字段不再赋值. @JsonBackReference 只是遇到recursive只会序列化一遍,  序列化过的不会再循环序列化了, 字段还是会赋值. 原文地址:https://www.cnblogs.com/smileblogs/p/12341114.html

JPA一对多循环引用的解决

说是解决,其实不是很完美的解决的,写出来只是想记录一下这个问题或者看一下有没有哪位仁兄会的,能否知道一二. 下面说说出现问题: 问题是这样的,当我查询一个一对多的实体的时候,工具直接就爆了,差不多我就猜到是哪里死循环了,最后等了好久,查看原因,果然是堆溢出,再然后是jsckson的错误.那么必然是序列化的问题了. 这是jackson的错误: at java.security.AccessController.doPrivileged(Native Method) at java.net.URLC

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

Json返回递推异常

org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]-> at org.springframework.http.converter.json.AbstractJackson2HttpMes

SpringMvc返回Json调试

spring-web-5.0.6.RELEASE.jar!/org/springframework/web/method/support/HandlerMethodReturnValueHandlerComposite.class public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebR

hibernate懒加载导致jackjson解析json时StackOverFlow

@JsonIgnore @JsonFilter @JsonBackReference @JsonManagedReference @JsonIgnoreProperties jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归(infinite recursion)问题.这些标注均可用在属性或对应的get.set方法中. @JsonBackReference和@JsonManag

Android Json处理之Gson

概述: 项目地址:https://github.com/google/gson Gson:java的json解析库.(其他类似的有json-lib,Jackson,fastson) 核心类:Gson或者GsonBuilder 使用jsonschema2pojo来创建POJO 1.通过网站http://www.jsonschema2pojo.org/在线创建:选择源代码类型为Json,注解类型是Gson,然后点击preview. 2.通过命令行工具:jsonschema2pojo.bat -a G

Recursion

Recursion It is legal for one function to call another; it is also legal for a function to call itself. It may not be obvious why what is a good thing, but it turns out to be one of the most magical things a program can do. For example: If a recursio

JI_4

(1) OOP and Design Patterns (1.1) Please explain difference among class, interface and abstract class. When you would use it rather than other? Feature Interface Abstract class Multiple inheritance A class may implement multiple interfaces. A class m