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

Guide to JUnit 5 Functional Interfaces

In this article, we will get familiar with JUnit 5 functional interfaces. JUnit 5 significantly advanced from its predecessors. Features like functional interfaces can greatly simplify our work once we grasp their functionality.

Read more

Getting Started with Spring Security and JWT

Spring Security provides a comprehensive set of security features for Java applications, covering authentication, authorization, session management, and protection against common security threats such as CSRF (Cross-Site Request Forgery).

Read more

Creating and Publishing an NPM Package with Automated Versioning and Deployment

In this step-by-step guide, we’ll create, publish, and manage an NPM package using TypeScript for better code readability and scalability. We’ll write test cases with Jest and automate our NPM package versioning and publishing process using Changesets and GitHub Actions.

Read more