Skip to content
Advertisement

Jackson serializes a ZonedDateTime wrongly in Spring Boot

I have a simple application with Spring Boot and Jetty. I have a simple controller returning an object which has a Java 8 ZonedDateTime:

JavaScript

In my RestController I simply have:

JavaScript

I was expecting the ZonedDateTime to be formatted according to the ISO format, but instead I am getting a whole JSON dump of the class like this:

JavaScript

I just have a spring-boot-starter-web application, using spring-boot-starter-jetty and excluding spring-boot-starter-tomcat.

Why is Jackson behaving like this in Spring Boot?

** UPDATE **

For those looking for a full step by step guide how to solve this I found this after asking the question: http://lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/

Advertisement

Answer

There is a library jackson-datatype-jsr310. Try it.

This library covers new datetime API and includes serializers for ZonedDateTime too.

All you need is just to add JavaTimeModule:

JavaScript

UPDATE

To convert datetime to ISO-8601 string you should disable WRITE_DATES_AS_TIMESTAMPS feature. You can easily do by either overriding ObjectMapper bean or by using application properties:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement