In spring boot mvc project with pure java configuration how to configure Jackson to omit lazy load Attributes
6 Answers
With recent versions of Spring Boot this is much easier.
Any beans of type com.fasterxml.jackson.databind.Module will be automatically registered with the auto-configured Jackson2ObjectMapperBuilder and applied to any ObjectMapper instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.
74.3 Customize the Jackson ObjectMapper
First ensure you have the required Jackson dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
</dependency>
You can then just include the module as a @Bean
in the application context.
@Bean
public Module hibernate4Module()
{
return new Hibernate4Module();
}

- 7,914
- 5
- 40
- 43
-
yer a blessed saint. was not working in this app till i switched to this config approach :O – Scott Langeberg Jun 13 '17 at 19:51
I user 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.
}
}
}
}
-
2Welcome to Stack Overflow! When posting an answer, make sure you're explaining what the problem is and how it needs to be fixed, not just posting code. [How to Answer] (https://stackoverflow.com/help/how-to-answer) is a great document about how to write a good answer on Stack Overflow. – 3ocene Jan 29 '19 at 02:05
-
1(Interestingly, SpringBoot docs says it can register the module if it is provided by a @Bean annotated method but) for me, none of the solutions except this one works. – emrekgn Jun 25 '19 at 11:47
If you are using SpringBoot, ideally you should already have Hibernate4Module. Else add this dependency
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.5.3</version>
</dependency>
Next create a class called "HibernateAwareObjectMapper" or whatever you want to name it:
with 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 your HibernateAwareObjectMapper through a message Converter. For this create a Config class that extens extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter. (If you already have one just follow the next step).
Now register the MessageConverter using 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) way of doing this for a spring boot web app.
Feel free to comment if you want to add to Answer.

- 1,445
- 1
- 15
- 20
-
just 1 small thing....make sure you're extending org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter and not the spring boot's one. – tomerz Sep 22 '16 at 04:50
-
But spring5 WebMvcConfigurerAdapter is deprecated... is there an update to this answer? – Eric Huang May 28 '18 at 18:01
For me the easiest way to achieve this was to extend WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter
and override extendMessageConverters
method. Inside I've searched for the MappingJackson2HttpMessageConverter
and just registered 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 by Spring.

- 346
- 5
- 10
-
Thanks @Grzegorz, you bring me out of a 2 days hell! It' worked perfectly for me. I'm in RestController domain, with hibernate 5. Json Jackson automatic serialization done by Spring, Hibernate Session and FetchType LAZY or EAGER now work togheter in the desiderable way. N.B.: now is not necessary any @JsonInclude(JsonInclude.Include.NON_ABSENT/NON_EMPTY/etc..) – Andrea Scarafoni Feb 07 '18 at 12:23
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = jsonConverter.getObjectMapper();
objectMapper.registerModule(new Hibernate5Module());
return jsonConverter;
}

- 67
- 2
-
Got this org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernate5Module' defined in com.example.app.In28minutesHierarchiesEntitiesApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.Module]: Factory method 'hibernate5Module' threw exception; nested exception is java.lang.Error: Unresolved compilation problem: Hibernate5Module cannot be resolved to a type – Eric Huang May 28 '18 at 18:03
What @Grzegorz wrote is perfect for me. Just to present here the his solution without his custom classes:
edit: i'm in 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.
}
}
}
}

- 937
- 2
- 13
- 26