Serializing LocalDate to JSON in Spring Boot

Serializing LocalDate to JSON in Spring Boot

Table Of Contents

Today, I stumbled (once again) over LocalDate in a Spring Boot application. LocalDate came with Java 8 and is part of the new standard API in Java for working with dates. However, if you want to effectively use LocalDate over Date in a Spring Boot application, you need to take some extra care, since not all tools support LocalDate by default, yet.

Serializing LocalDate with Jackson

Spring Boot includes the popular Jackson library as JSON (de-)serializer. By default, Jackson serializes a LocalDate object to something like this:

{
  "year": 2017,
  "month": "AUGUST",
  "era": "CE",
  "dayOfMonth": 1,
  "dayOfWeek": "TUESDAY",
  "dayOfYear": 213,
  "leapYear": false,
  "monthValue": 8,
  "chronology": {
      "id":"ISO",
      "calendarType":"iso8601"
   }
}

That’s a very verbose representation of a date in JSON, wouldn’t you say? We’re only really interested in the year, month and day of month in this case, so that’s exactly what should be contained in the JSON.

The Jackson JavaTimeModule

To configure Jackson to map a LocalDate into a String like 1982-06-23, you need to activate the JavaTimeModule. You can register the module with a Jackson ObjectMapper instance like this:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

The module teaches the ObjectMapper how to work with LocalDates and the parameter WRITE_DATES_AS_TIMESTAMPS tells the mapper to represent a Date as a String in JSON.

The JavaTimeModule is not included in Jackson by default, so you have to include it as a dependency (gradle notation):

compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.6"

Mapping LocalDate in a Spring Boot application

When using Spring Boot, an ObjectMapper instance is already provided by default (see the reference docs on how to customize it in detail).

However, you still need to add the dependency to jackson-datatype-jsr310 to your project. The JavaTimeModule is then activated by default. The only thing left to do is to set the following property in your application.yml (or application.properties):

spring:
  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
Written By:

Tom Hombergs

Written By:

Tom Hombergs

As a professional software engineer, consultant, architect, general problem solver, I've been practicing the software craft for more than fifteen years and I'm still learning something new every day. I love sharing the things I learned, so you (and future me) can get a head start. That's why I founded reflectoring.io.

Recent Posts

Build CRUD APIs Using Apollo Server(Graphql), MongoDB and Node.Js

REST API is a widely used client-server communication protocol, but it has limitations when dealing with clients such as web, iOS, Android, smart devices, etc.

Read more

Getting started with Spring Security and Spring Boot

Spring Security is a framework that helps secure enterprise applications. By integrating with Spring MVC, Spring Webflux or Spring Boot, we can create a powerful and highly customizable authentication and access-control framework.

Read more

Demystifying Transactions and Exceptions with Spring

One of the most convincing justifications for using the Spring Framework is its extensive transaction support. For transaction management, the Spring Framework offers a stable abstraction.

Read more