【问题标题】:Spring Boot and Jackson, JSR310 in response bodySpring Boot 和 Jackson,响应正文中的 JSR310
【发布时间】:2014-12-07 10:06:10
【问题描述】:

JacksonAutoConfiguration 使用已注册的 JSR310Module 初始化 ObjectMapper。然后 HttpMessageConvertersAutoConfiguration 初始化一个 MappingJackson2HttpMessageConverter。但是这个 MessageConverter 永远不会被插入 RequestResponseBodyMethodProcessor。

结果:

  • 如果控制器返回一个应该由 Jackson json 化的对象,则日期转换不佳,因为使用了错误的 MappingJackson2HttpMessageConverter(在 WebMvcConfigurationSupport#addDefaultHttpMessageConverters 中初始化)
  • 但是,如果将ObjectMapper注入到控制器中,用来对同一个对象进行json化,那么日期格式就可以了。

如何唤醒 WebMvcAutoConfigurationAdapter?我应该怎么做才能将配置的 Jackson ObjectMapper 与 Spring MVC 绑定并使其处理响应体?

【问题讨论】:

  • 您是否关闭了 Spring Boot Web 配置?如果您使用 @EnableWebMvc 可以解释您的症状。
  • 就是这样。所以规则是:不要在 Spring Boot 中使用 @EnableWebMvc,除非你不想自定义 Web MVC 配置?
  • 是的,这是一个很好的经验法则。

标签: spring-boot


【解决方案1】:

如果您使用 @EnableWebMvc 可以解释您的症状 所以规则是:不要在 Spring Boot 中使用 @EnableWebMvc,除非您不想自定义 Web MVC 配置?

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 2016-09-24
    • 2019-01-13
    • 2016-11-14
    • 2020-08-26
    • 2022-01-13
    • 2016-05-17
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多