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.
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:
But use ObjectMapper and other Jackson classes from Jackson 2.0 instead of Jackson 1.*