Good afternoon, I spent a lot of time on Googlin and switched from JSON 1 to JSON 2, but when I try to return a list of entities, I get an exception:
"Exception received Failed to write JSON: failed to initialize proxy - no session (via chain of links:"
I know that this is due to the lazy initialization of Hibernate, but I did not understand how to fix it. I thought the jackson-hibernation solution is a solution. However, I could not get it to work.
Here is my technique:
- Spring: 3.2.1RELEASE
- Hibernate 4.1.7.Final
- Jackson 2
- jackson-core 2.0.4
- jackson-databind: 2.0.4
- jackson-datatype-hibernate4: 2.1.2
I am using Java Config: ...
@Bean public com.mycompany.config.MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); mappingJackson2HttpMessageConverter.setObjectMapper(new HibernateAwareObjectMapper()); return mappingJackson2HttpMessageConverter; } @Bean public OpenEntityManagerInViewFilter springOpenEntityManagerInViewFilter() { return new org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter(); } @Bean public ContentNegotiatingViewResolver contentNegotiatingViewResolver() { final ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver(); contentNegotiatingViewResolver.setDefaultContentType(MediaType.APPLICATION_JSON); final ArrayList defaultViews = new ArrayList(); defaultViews.add(converter()); contentNegotiatingViewResolver.setDefaultViews(defaultViews); return contentNegotiatingViewResolver; }
I also have:
public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { Hibernate4Module hm = new Hibernate4Module(); registerModule(hm); } }
A thread starts with a controller that calls several services, each of which returns an object or list. Then I put the returned objects in the object and return it, thinking that @ResponseBody will work. However, I should not have things connected correctly, as I am still getting exceptions.
Can anyone see any errors here?
thankful ...
source share