Override Default ObjectMapper in Spring Boot

In this story, we will discuss customizing the ObjectMapper in Spring Boot Rest APIs. This is very important to control the data flow in and out in the spring boot application.

Image for post
Image for post

In Spring Boot, mostly the response is in the format of JSON. In most cases, unnecessary information is received in response like null and default values. This is an overhead data transfer to the caller. This is one of the scenarios, another one could be customizing date format for LocalDate, LocalDateTime, and ZonedDatetime.

public class CustomObjectMapper extends ObjectMapper {

public CustomObjectMapper() {
configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
registerModule(new JavaTimeModule());

The Above is the Custom Object Mapper, overridden the ObjectMapper. If you observe, we have enabled and disabled serialization features. You can explore all the other features here.

In order to let Spring aware of the custom object mapper we have to implement and override the MessageConverters.

public class ApplicationConfiguration implements WebMvcConfigurer {
@Bean(name = "jsonMapper")
public ObjectMapper jsonMapper() {
return new CustomObjectMapper();
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter(jsonMapper()));

That’s all you have to do for customizing ObjectMapper in Spring Boot.

Written by

Experience in Open source development, Technical Leader. Expert in Java/J2EE, Integration, analytics. Loves Cricket, cooking, movies and travelling.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store