2017-07-25 00:00:00 +0000

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

Follow me on Twitter for more tips on how to become a better software developer.

Get My Book for just $5!

Get Your Hands Dirty on Clean Architecture

Liked this article? Subscribe to my mailing list to get notified about new content and get my eBook "Get Your Hands Dirty on Clean Architecture" for just $5!

Grab My Book for Just $5!

Get Your Hands Dirty on Clean Architecture

Rated 4.8 stars on Amazon and 4.6 stars on Goodreads!

Subscribe to my Mailing List and get my book Get Your Hands Dirty on Clean Architecture for just $5!

Grab My Book for Just $5!

Get Your Hands Dirty on Clean Architecture

Rated 4.8 stars on Amazon and 4.6 stars on Goodreads!

Subscribe to my Mailing List and get my book Get Your Hands Dirty on Clean Architecture for just $5!

Follow Reflectoring