Configure Jackson for Lazy Boot Attributes in Spring Boot

In spring, download the mvc project with a clean java configuration, how to configure Jackson for no lazy load Attributes

+6
jackson spring-boot hibernate lazyload
source share
6 answers

If you are using SpringBoot, ideally you should already have a Hibernate4Module . Otherwise add this dependency

<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate4</artifactId> <version>2.5.3</version> </dependency> 

Then create a class called "HibernateAwareObjectMapper" or whatever you call:

with the following contents:

 import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { registerModule(new Hibernate4Module()); } } 

for different versions of Hibernate, refer to these Hibernate modules:

 // for Hibernate 4.x: mapper.registerModule(new Hibernate4Module()); // or, for Hibernate 5.x mapper.registerModule(new Hibernate5Module()); // or, for Hibernate 3.6 mapper.registerModule(new Hibernate3Module()); 

Now you need to register HibernateAwareObjectMapper through the message converter. To do this, create a Config class that extends the WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter extension. (If you already have one, just follow the next step).

Now register MessageConverter with HibernateObjectMapper:

 @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); } 

Viola!!! That should be enough. This is the pure-java (no-xml) method for this spring web application.

Do not forget to comment if you want to add an answer.

+6
source share

In recent versions of Spring Boot, this is much simpler.

Any beans like com.fasterxml.jackson.databind.Module will be automatically registered using the autoconfigured Jackson2ObjectMapperBuilder and apply to any ObjectMapper instances that it creates. This provides a global mechanism for adding custom modules when adding new features to your application.

74.3 Configure Jackson ObjectMapper

First, make sure you have the necessary dependency on Jackson:

 <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate4</artifactId> </dependency> 

Then you can simply enable the module as @Bean in the application context.

 @Bean public Module hibernate4Module() { return new Hibernate4Module(); } 
+11
source share

For me, the easiest way to achieve this was to extend the WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter method and extendMessageConverters overrides. Inside, I searched for MappingJackson2HttpMessageConverter and just registered the Jackson Hibernate module.

 @Configuration @AutoConfigureAfter(DispatcherServletAutoConfiguration.class) public class CustomWebMvcAutoConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { for (HttpMessageConverter converter : converters) { if (converter instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); mapper.registerModule(new Hibernate4Module()); // replace Hibernate4Module() with the proper class for your hibernate version. } } } } 

This way you won’t lose all the default converters configured with Spring.

+2
source share
 @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = jsonConverter.getObjectMapper(); objectMapper.registerModule(new Hibernate5Module()); return jsonConverter; } 
+1
source share

I use springboot and hibernamte5.0.x. It works!

1. pom.xml

 <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate5</artifactId> <version>2.8.4</version> </dependency> 

2. Webconfig

 @Configuration public class WebConfig implements WebMvcConfigurer { public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { for (HttpMessageConverter converter : converters) { if (converter instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); mapper.registerModule(new Hibernate5Module()); // replace Hibernate4Module() with the proper class for your hibernate version. } } } } 
+1
source share

What @Grzegorz wrote is perfect for me. Just to present your solution here without its custom classes:

edit: I'm in a RestController domain

 @Configuration public class CustomWebMvcAutoConfig extends WebMvcConfigurerAdapter { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { for (HttpMessageConverter converter : converters) { if (converter instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); mapper.registerModule(new Hibernate5Module()); // replace Hibernate4Module() with the proper class for your hibernate version. } } } } 
0
source share

All Articles