In such a scenario, you should ideally use the Hibernate4Module ( Hibernate4Module github Link )
Using this ensures that serialization of entities in JSON on the spring dormant layer is related to lazy loaded attributes and will not try to access them (or serialize them in JSON in your case).
I will give a list of possible solutions below.
If you use maven, these will be your dependencies:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate4</artifactId> <version>2.3.0</version> </dependency>
Create the HibernateAwareObjectMapper class and register the Hibernate4Module in it.
package com.mypackage.web; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; public class HibernateAwareObjectMapper extends ObjectMapper { private static final long serialVersionUID = 1L; public HibernateAwareObjectMapper() { registerModule(new Hibernate4Module()); } }
If you are using spring beans xml configuration, you can use something like:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="..."> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.mypackage.web.HibernateAwareObjectMapper"/> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
else, if you are using a pure java based configuration, as when loading spring, you can do it like this:
@Configuration @EnableWebMvc @EnableAsync @ComponentScan(basePackages = { "com.mypackage.controller" }) // package referring to controllers @PropertySource("classpath:imagesConfig.properties") public class WebConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters){ List<MediaType> supportedMediaTypes=new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.TEXT_PLAIN); MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(new HibernateAwareObjectMapper()); converter.setPrettyPrint(true); converter.setSupportedMediaTypes(supportedMediaTypes); converters.add(converter); super.configureMessageConverters(converters); } }
Then your controllers might look something like this:
@RequestMapping(value="/doSomething", method=RequestMethod.POST, produces="application/json;charset=UTF-8") public @ResponseBody MyCustomWebResponseObject<MyEntity> create(@Valid @RequestBody MyEntity myEntity) throws Exception {
Also now, if you want to still go through the lazy loaded attribute of the object, then in your service / DAO layer or layer annotated by @Transactional
You can do it:
Hibernate.initialize(myEntity.getLazyLoadedAttribute());
Hope this helps :)
do upvote and mark this as an answer if my answer helps you :)