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

霸气de小男生 提交于 2019-11-27 03:18:14

问题


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"

回答1:


You can set timezone for whole application with adding this to a config class:

@PostConstruct
void started() {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}



回答2:


I found myself with the same problem. In my case, I have only one timezone for my app, then adding:

spring.jackson.time-zone: America/Sao_Paulo

in my application.properties solved the problem.

Source: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#JACKSON




回答3:


Solved registering a Jackson2ObjectMapperBuilderCustomizer bean:

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


来源:https://stackoverflow.com/questions/46151633/how-to-make-default-time-zone-apply-in-spring-boot-jackson-date-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!