Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Spring Boot – Customize the Jackson ObjectMapper

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this article, we will take a look at the most common ways to configure the serialization and deserialization options.

Let us do go through the default configuration. So by default, the Spring Boot configuration will be as follows:

  • Disable MapperFeature.DEFAULT_VIEW_INCLUSION
  • Disable DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES
  • Disable SerializationFeature. WRITE_DATES_AS_TIMESTAMPS

Let’s start with a quicker example by implementing the same


  • The client sends a GET request to our/coffee?name=Lavazza
  • The controller will return a new Coffee object
  • Spring will use customization options by using String and LocalDateTime objects

It is as follows, so primarily we need to create a class named Coffee which is as follows:

// Class
public class Coffee {

  // Getters and setters
  private String name;
  private String brand;
  private LocalDateTime date;
  • Now we will also be defining a simple REST controller to demonstrate the serialization:
@GetMapping ("/coffee")

public Coffee getCoffee(
  @RequestParam(required =  false) String brand,
  @RequiredParam(required = false) String name) {

  return new Coffee()
  • By default, the response when calling GET http://localhost:8080/coffee?brand=lavazza will be as follows:
  "name": null,
  "brand": Lavazza",
  "date": "2020 - 11 - 16T10: 21: 35.974"
  • Now We would like to exclude null values and to have a custom date format (dd-MM-yyy HH:mm). The final response will be as follows:
  "brand:" "Lavazza",
  "date": "04-11-20202 10:34"
  • When using Spring Boot, we have the option to customize the default ObjectMapper or to override it. We’ll cover both of these options in the next sections.

Now let us roll to eccentric point in article by customizing the Default Object Mapper. Here. we will be discussing how to customize the default ObjectMapper that Spring Boot uses.

Application Properties and Custom Jackson Module

The simplest way to configure the mapper is via application properties.  The general structure of the configuration is as follows:

  spring.jackson.<category_name>.<feature_name>=true, false

As an example, if we want to disable SerializationFeature. WRITE_DATES_AS_TIMESTAMPS, we’ll add:


Besides the mentioned feature categories, we can also configure property inclusion:

spring.jackson.default-property-inclusion=always, non_null, non_absent, non_default, non_empty

Configuring the environment variables in the simplest approach. The downside of this approach is that we can’t customize advanced options like having a custom date format for LocalDateTime. At this point, we’ll obtain the result which is shown below:

  "brand": "Lavazza",
  "date": "2020-11-16T10:35:34.593"

Now in order to achieve our goal, we’ll register a new JavaTimeModule with our custom date format:


// Class
public class CoffeeRegisterModuleConfig {


  public Module javaTimeModule() {

    JavaTimeModule module = new JavaTimeModule();

    return module;


The purpose of this functional interface is to allow us to create configuration beans. They will be applied to the default ObjectMapper created via Jackson2ObjectMapperBuilder.

public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
  return builder ->

The configuration beans are applied in a specific order, which we can control using the @Order annotations. This elegant approach is suitable if we want to configure the ObjectMapper from different configurations or modules.


Another clean approach is to define a Jackson2ObjectMapperBuilder bean. Actually, Spring Boot is using this builder by default when building the ObjectMapper and will automatically pick up the defined one:

public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
  return new

It will configure two options by default:

  • Disable MapperFeature.DEFAULT_VIEW_INCLUSION
  • Disable DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES

According to the Jackson2ObjectMapperBuilder documentation, it will also register some modules if they’re present on the classpath:

  • jackson-datatype-jdk8:support for other Java 8 types like Optional
  • jackson-datatype-jsr310:support for Java 8 Date and Time API types
  • jackson-datatype-joda:support for Joda-Time types
  • jackson-module-kotlin:support for Kotlin classes and data classes

Note: The advantage of this approach is that the Jackson2ObjectMapperBuilder offers a simple and intuitive way to build an ObjectMapper.

We can just define a bean with the type MappingJackson2HttpMessageConverter, and Spring Boot will automatically use it:


// Convertor method
public MappingJackson2HttpMessageConverter() {

  Jackson2ObjectMapperBuilder builder = new

  return new MappingJackson2HttpMessageConverter(;

Note: Make sure to check out our Spring Http Message Converters article to learn more.

Testing the Configuration

Lastly, in order to test our configuration, we’ll use TestRestTemplate and serialize the objects as String. In this way, we can validate that our Coffee object is serialized without null values and with the custom date format:


public void whenGetCoffee_thenSerializedWithDateAndNonNull() {

  String formattedDate =

// Our strings
  String brand = "Lavazza";
  String url = "/coffee?branf=" + brand;
  String response = restTemplate.getForObject(url, String.class);

  assertThat(response).isEqualTo("{"brand\":\"" + brand +
                                "\",\"date\":\"" + formatedDate + "\"}");

Conclusion: We took a look at several methods to configure the JSON serialization options when using Spring Boot. Here we saw two different approaches:configuring the default options or overriding the default configuration.

My Personal Notes arrow_drop_up
Last Updated : 17 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials