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

Optimizing Node.js Application Performance with Caching

Endpoints or APIs that perform complex computations and handle large amounts of data face several performance and responsiveness challenges. This occurs because each request initiates a computation or data retrieval process from scratch, which can take time.

Read more

Bubble Sort in Kotlin

Bubble Sort, a basic yet instructive sorting algorithm, takes us back to the fundamentals of sorting. In this tutorial, we’ll look at the Kotlin implementation of Bubble Sort, understanding its simplicity and exploring its limitations.

Read more

Quick Sort in Kotlin

Sorting is a fundamental operation in computer science and Quick Sort stands out as one of the most efficient sorting algorithms.

Read more