Spring @ResponseBody Json Cyclic Reference [英] Spring @ResponseBody Json Cyclic Reference

查看:151
本文介绍了Spring @ResponseBody Json Cyclic Reference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring 3.x @ResponseBody 生成json / xml响应,当有很多关系b / w时,我正在使用JPA 2.0 ORM表然后json抛出LazyInitializationException

I am trying to use Spring 3.x @ResponseBody to generate json/xml response, I am using JPA 2.0 ORM when there is many-many relation b/w tables then json is throwing LazyInitializationException

如果我给出eager fetch,那么它将进入循环引用。

If I give "eager fetch" then it is going into cyclic reference.

推荐答案

我最近遇到了类似的问题: Jackson - 具有双向关系的实体的序列化(避免循环)

I recently encountered a similar problem: Jackson - serialization of entities with birectional relationships (avoiding cycles)

所以解决方案是升级到Jackson 2.0,并添加到类以下注释:

So the solution is to upgrade to Jackson 2.0, and add to classes the following annotation:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, 
                  property = "@id")
public class SomeEntityClass ...

然后问题是Spring无法与Jackson合作2.0。这已通过以下方式解决:

Then the problem is that Spring doesn't work with Jackson 2.0. This has been solved in the following way:

<bean id="jacksonMessageConverter"
          class="own.implementation.of.MappingJacksonHttpMessageConverter"/>

<bean class="org.springframework.web.servlet.mvc
             .annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
        <property name="requireSession" value="false"/>
    </bean>

own.implementation.of.MappingJacksonHttpMessageConverter 基于此:

http:// www。 jarvana.com/jarvana/view/org/springframework/spring-web/3.0.0.RELEASE/spring-web-3.0.0.RELEASE-sources.jar!/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java ?format = ok

但是使用来自Jackson 2.0的ObjectMapper和其他Jackson类而不是Jackson 1。*

But use ObjectMapper and other Jackson classes from Jackson 2.0 instead of Jackson 1.*

这篇关于Spring @ResponseBody Json Cyclic Reference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆