我在Spring Boot配置中设置了spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
,但Jackson序列化程序仍然为[1942,4,2]
值生成"1942-04-02"
而不是DateTime
。
一些调试快照
在org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration.StandardJackson2ObjectMapperBuilderCustomizer#customize
那里
configureFeatures(builder, this.jacksonProperties.getSerialization());
显示" WRITE_DATES_AS_TIMESTAMPS" - > "假"
稍后在org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#configure
中出现了这个循环
for (Object feature : this.features.keySet()) {
configureFeature(objectMapper, feature, this.features.get(feature));
}
再次this.features
说" WRITE_DATES_AS_TIMESTAMPS" - > "假"
然而,在序列化DateTime
com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#useTimestamp
期间,由于provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
返回false,因此显示为false。
尝试修复
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
替换为spring.jackson.serialization.write-dates-as-timestamps=false
,因为我发现很多地方都提到了这一点(即使Boot documentation没有暗示这一点)。那这个呢?它们似乎是同义词 - 没有效果。WebMvcConfigurationSupport
替换为WebMvcConfigurerAdapter
。虽然这确实有帮助,但我无法理解为什么会如此。答案 0 :(得分:10)
Spring Boot存在WebMvcConfigurationSupport
bean,表示您希望完全控制Spring MVC的配置。您通常使用@EnableWebMvc
来结束这样的bean,但您也可以声明自己的bean或WebMvcConfigurationSupport
的配置类。
如果您继承WebMvcConfigurerAdapter
而不是WebMvcConfigurationSupport
,那么您可以对Spring Boot的Spring MVC自动配置进行附加更改,而不是完全接管。
Spring Boot的Spring MVC自动配置的一部分是将其配置为使用自动配置的ObjectMapper
进行HTTP消息转换。如果关闭Boot的Spring MVC自动配置,它将使用自己独立的ObjectMapper
,不受任何spring.jackson.*
配置设置的影响。