Search code examples
springspring-mvcspring-bootjacksonjackson2

How to make default time zone apply in Spring Boot Jackson Date serialization


I have configured my Spring Boot application to serialize dates as ISO8601 strings:

spring:
  jackson:
    serialization:
      write-dates-as-timestamps: false

This is what I am getting:

"someDate": "2017-09-11T07:53:27.000+0000"

However my time zone is Europe/Madrid. In fact if I print TimeZone.getDefault() that's what I get.

How can I make Jackson serialize those datetime values using the actual timezone? GMT+2

"someDate": "2017-09-11T09:53:27.000+0200"

Solution

  • Solved registering a Jackson2ObjectMapperBuilderCustomizer bean:

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder -> 
            jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
    }