问题
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
If I give "eager fetch" then it is going into cyclic reference.
回答1:
I recently encountered a similar problem: Jackson - serialization of entities with birectional relationships (avoiding cycles)
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 ...
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>
And the own.implementation.of.MappingJacksonHttpMessageConverter
is based on this:
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.*
回答2:
Judging by your comments, just create a custom Serializer
.
Your 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
.
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.
<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" />
回答3:
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. 2 choices:
- Initialize all of the objects' child associations in the DAO layer
- Convert the ORM-managed object to a TO and pass that to the Controller for conversion to JSON
来源:https://stackoverflow.com/questions/9727279/spring-responsebody-json-cyclic-reference