设置Jackson功能WRITE_DATES_AS_TIMESTAMPS无法在Spring Boot中运行 [英] Setting Jackson feature WRITE_DATES_AS_TIMESTAMPS not working in Spring Boot

查看:5804
本文介绍了设置Jackson功能WRITE_DATES_AS_TIMESTAMPS无法在Spring Boot中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Boot配置中设置 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false 但Jackson序列化程序仍然生成 [1942,4,2 ] 代替1942-04-02获取 DateTime 值。

I set spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false in the Spring Boot config but the Jackson serializer still produces [1942,4,2] instead of "1942-04-02" for a DateTime value.

一些调试快照


  • org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration.StandardJackson2ObjectMapperBuilderCustomizer#customize

configureFeatures(builder,this.jacksonProperties.getSerialization());

显示WRITE_DATES_AS_TIMESTAMPS - >false

which shows that "WRITE_DATES_AS_TIMESTAMPS" -> "false"

稍后在 org.springframework.http.converter.json.Jackson2ObjectMapperBuilder #configure 有这个循环

for(对象特征:this.features.keySet()){
configureFeature(objectMapper,feature,this.fe) atures.get(特征));
}

再次 this.features 说WRITE_DATES_AS_TIMESTAMPS - >false

and again this.features says "WRITE_DATES_AS_TIMESTAMPS" -> "false"

但在序列化 DateTime com.fasterxml期间.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase #useTimestamp 表示为false,因为 provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)返回false。

Yet during serialzation of a DateTime com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#useTimestamp says false because provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) returns false.

尝试修复


  • 出于绝望,我用 spring.jackson.serialization.write-dates-as替换了 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false -timestamps = false 因为我发现在很多地方提到过(即使引导文档没有暗示这一点)。那这个呢?它们似乎是同义词 - 没有效果。

  • 在写这个问题时,建议 WRITE_DATES_AS_TIMESTAMPS不要上电Spring boot 1.3.5 。答案是用 WebMvcConfigurerAdapter 替换 WebMvcConfigurationSupport 。虽然这确实有帮助,但我确实无法理解为什么会如此。

  • Out of despair I replaced spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false with spring.jackson.serialization.write-dates-as-timestamps=false because I found that mentioned in a lot of places (even though the Boot documentation doesn't hint at this). What about this? They seem to be synonyms - no effect.
  • While writing this question SO suggested WRITE_DATES_AS_TIMESTAMPS not woking on Spring boot 1.3.5. The answer says to replace WebMvcConfigurationSupport with WebMvcConfigurerAdapter. While this does help indeed I fail to understand why so.

推荐答案

Spring Boot采用了存在一个 WebMvcConfigurationSupport bean表示您希望完全控制Spring MVC的配置。您通常使用 @EnableWebMvc 来结束这样的bean,但您也可以声明自己的bean或配置类,它是 WebMvcConfigurationSupport

Spring Boot takes the presence of a WebMvcConfigurationSupport bean as an indication that you want to take complete control of the configuration of Spring MVC. You'd typically end up with such a bean by using @EnableWebMvc but you could also declare your own bean or configuration class that is a WebMvcConfigurationSupport.

如果您继承 WebMvcConfigurerAdapter 而不是 WebMvcConfigurationSupport 你正在对Spring Boot的Spring MVC自动配置进行附加更改而不是完全接管。

If you subclass WebMvcConfigurerAdapter rather than WebMvcConfigurationSupport you're making an additive change to Spring Boot's auto-configuration of Spring MVC rather than taking over completely.

Spring Boot自动配置Spring的一部分MVC将其配置为使用自动配置的 ObjectMapper 进行HTTP消息转换。如果关闭Boot的Spring MVC自动配置,它将使用自己独立的 ObjectMapper ,它不受任何 spring.jackson。*的影响。 / code>配置设置。

Part of Spring Boot's auto-configuration of Spring MVC is to configure it to use the auto-configured ObjectMapper for HTTP message conversion. If you switch off Boot's auto-configuration of Spring MVC, it will use its own, separate ObjectMapper that is unaffected by any spring.jackson.* configuration settings.

这篇关于设置Jackson功能WRITE_DATES_AS_TIMESTAMPS无法在Spring Boot中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆