如何在 Spring Boot Jackson 日期序列化中应用默认时区 [英] How to make default time zone apply in Spring Boot Jackson Date serialization
本文介绍了如何在 Spring Boot Jackson 日期序列化中应用默认时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将 Spring Boot 应用程序配置为将日期序列化为 ISO8601 字符串:
I have configured my Spring Boot application to serialize dates as ISO8601 strings:
spring:
jackson:
serialization:
write-dates-as-timestamps: false
这是我得到的:
"someDate": "2017-09-11T07:53:27.000+0000"
但是我的时区是欧洲/马德里.事实上,如果我打印 TimeZone.getDefault()
这就是我得到的.
However my time zone is Europe/Madrid. In fact if I print TimeZone.getDefault()
that's what I get.
如何让 Jackson 使用实际时区序列化这些日期时间值?格林威治标准时间+2
How can I make Jackson serialize those datetime values using the actual timezone? GMT+2
"someDate": "2017-09-11T09:53:27.000+0200"
推荐答案
解决了注册 Jackson2ObjectMapperBuilderCustomizer bean 的问题:
Solved registering a Jackson2ObjectMapperBuilderCustomizer bean:
@Bean
public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
return jacksonObjectMapperBuilder ->
jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
}
这篇关于如何在 Spring Boot Jackson 日期序列化中应用默认时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文