java - Spring @ResponseBody Json 循环引用

标签 java spring spring-mvc jackson bidirectional-relation

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

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

最佳答案

我最近遇到了类似的问题:Jackson - serialization of entities with birectional relationships (avoiding cycles)

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

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

那么问题是 Spring 不适用于 Jackson 2.0。这已通过以下方式解决:

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

关于java - Spring @ResponseBody Json 循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727279/

相关文章:

spring-mvc - 没有可用于当前线程的实际事务的 EntityManager - 无法可靠地处理 'persist' 调用

java - Intellij 从 gui 表单构建项目时给我一个错误

database - 使用 Spring 3 和 Hibernate 3 的身份验证(基于注释)

java - Spring:根据配置文件加载其他属性

andOperator 的 Spring Mongo 标准问题

java - Spring Webflow 2 bean 定义的架构

java - 出现 OutOfMemoryError 后,Spring REST 服务不会关闭

java - 在 EntityMode.DOM4J session 中使用条件时出现 Hibernate NullPointerException

java - 期间更改 ImageIcon

java - Jersey 客户端将数据发送到 String