简体   繁体   English

Spring @ResponseBody Json Cyclic Reference

[英]Spring @ResponseBody Json Cyclic Reference

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 我试图使用Spring 3.x @ResponseBody生成json / xml响应,当有很多关系b / w表时我使用JPA 2.0 ORM然后json抛出LazyInitializationException

If I give "eager fetch" then it is going into cyclic reference. 如果我给出“渴望获取”,那么它将进入循环引用。

I recently encountered a similar problem: Jackson - serialization of entities with birectional relationships (avoiding cycles) 我最近遇到了类似的问题: Jackson - 具有双向关系的实体的序列化(避免循环)

So the solution is to upgrade to Jackson 2.0, and add to classes the following annotation: 因此解决方案是升级到Jackson 2.0,并向类中添加以下注释:

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

Then the problem is that Spring doesn't work with Jackson 2.0. 那么问题是Spring不适用于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>

And the own.implementation.of.MappingJacksonHttpMessageConverter is based on this: 而且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 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

But use ObjectMapper and other Jackson classes from Jackson 2.0 instead of Jackson 1.* 但是使用来自Jackson 2.0而不是Jackson 1的ObjectMapper和其他Jackson类。*

Judging by your comments, just create a custom Serializer . 根据您的评论判断,只需创建一个自定义Serializer

Your JsonSerializer . 你的JsonSerializer You can have these for each object type you're trying to serialize. 您可以为要尝试序列化的每种对象类型使用这些。

public class MyObjectJsonSerializer extends JsonSerializer<MyObject> {

@Override
public Class<MyObject> handledType() {
    return MyObject.class;
}

@Override
public void serialize(MyObject myObject, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    jgen.writeStartObject();
    jgen.writeNumberField("id", myObject.getId());
    // whatever else you need
    jgen.writeEndObject();
}

} }

Your ObjectMapper . 你的ObjectMapper

public class MyObjectMapper extends ObjectMapper {

public MyObjectMapper() {
    SimpleModule module = new SimpleModule("My Module", new Version(1, 0, 0, "SNAPSHOT"));
    module.addSerializer(new MyObjectJsonSerializer());

    this.registerModule(module);
}

} }

And then in your spring-config.xml. 然后在你的spring-config.xml中。

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper" ref="myObjectMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="myObjectMapper" class="com.manne.app.objectmapper.MyObjectMapper" />

Sounds like you are serializing an ORM-managed object to JSON, but haven't initialized all of the child associations, leading to the LazyInitializationException , as your Controller doesn't have a handle to the DB connection. 听起来您正在将ORM管理的对象序列化为JSON,但尚未初始化所有子关联,导致LazyInitializationException ,因为您的Controller没有数据库连接的句柄。 2 choices: 2种选择:

  1. Initialize all of the objects' child associations in the DAO layer 初始化DAO层中的所有对象的子关联
  2. Convert the ORM-managed object to a TO and pass that to the Controller for conversion to JSON 将ORM管理的对象转换为TO并将其传递给Controller以转换为JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM