Serialize Java 8 LocalDate With Jackson In Spring Boot

This tutorial is going to illustrate how to serialize Java 8 LocaleDate with Jackson in Spring Boot. Besides, we also get through how to serialize other types of Java 8 Date Time API.

1. Overview

Java 8 introduced us a new Date-Time API to work with date and time (known as JSR-310), and some of the typical types of the new API are LocalDate, LocalDateTime, ZonedDateTime. During the time we develop our software, we may want to serialize some objects contain those types into JSON using Jackson. However, Jackson core library doesn’t support the Java 8 new Date-Time API by default. Therefore, when Jackson serializes those types, it treats them as normal Java objects and results are not incorrect date-time formats. For example, let’s see how the current LocalDate (at the time writing) is serialized into JSON by Jackson:

The JSON is:

While the output we might want is just: “2017-12-30”.

To enable Jackson to support the new Java 8 Date Time API, we will need to use JavaTimeModule, a class that registers capability of serializing java.time objects with the Jackson core. And that class is included in the jackson-datatype-jsr310 module.

2. Prerequisites

2.1. Jackson Library Dependency

We will need to include Jackson core and jackson-datatype-jsr310 in our classpath. To use those with Maven:

2.2. POJO Class

For all examples in this tutorial, we will use a POJO called Member defined as follows:

3. Serialize Java 8 LocalDate With Jackson

Let’s see how we serialize Java 8 LocalDate with Jackson in the following example:

Firstly we need to initialize an ObjectMapper object. Then we will need to register it a JavaTimeModule object. And finally, we configure the value of the WRITE_DATES_AS_TIMESTAMPS feature to false which indicates that the dates object will be serialized as textual values instead of timestamps.

Running the example will give us the result:

4. Change Serialization Format Of LocalDate With Jackson

To change the serialization format of a LocalDate object with Jackson, we simply annotate the fields with @JsonFormat annotation and specify the desired date-time pattern. For example, let’s change the format of the birthDate field to yyyy/MM/dd.

Running the example again will give us the result:

5. Serialize Java 8 LocalDate With Jackson In Spring Boot

To serialize Java 8 LocalDate with Jackson and other types as well in Spring Boot, firstly, we need to include the jackson-datatype-jsr310  in the classpath, for example:

Secondly, we need to we configure the value of the WRITE_DATES_AS_TIMESTAMPS features to false in the application.properties file, for example:

Next, let’s see a sample Ret controller written in Spring Boot as follows:

When we make a request to the above API, the birthDate will be returned in the correct text format yyyy-MM-dd:

6. Conclusions

The tutorial has illustrated us how to serialize Java 8 LocaleDate with Jackson in Spring Boot. We got through both how to configure Jackson to work independently and to work with Spring Boot as well. Notice that the configuration also works for other types of Java 8 Date Time API such as LocalDateTime, ZonedDateTime, etc.

The sample source code presented in this tutorial is available on my Github project. It’s both a Maven and Gradle based project. Therefore, it’s is easy to be imported in IDE such as Eclipse, IntelliJ, etc.

Below are other related tutorials for your references:

Ignore Unknown Properties or New Fields in Jackson

How to Configure Logging in Spring Boot