【问题标题】:Spring web: @RequestBody Json conversion for Java8 LocalTime not workingSpring web:Java8 LocalTime 的@RequestBody Json 转换不起作用
【发布时间】:2015-02-08 22:26:21
【问题描述】:

在我的 Spring REST webapp 中,我试图让控制器使用 Java8 LocalTime。 我在 POST 请求中发送这个 Json

{
    "hour": "0",
    "minute": "0",
    "second": "0",
    "nano": "0"
}

我得到一个 HttpMessageNotReadableException 并出现以下杰克逊错误

Could not read JSON: No suitable constructor found for type [simple type, class java.time.LocalTime]: can not instantiate from JSON object (need to add/enable type information?) 
at [Source: org.apache.catalina.connector.CoyoteInputStream@58cfa2a0; line: 2, column: 5]; 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class java.time.LocalTime]: can not instantiate from JSON object (need to add/enable type information?) 
at [Source: org.apache.catalina.connector.CoyoteInputStream@58cfa2a0; line: 2, column: 5]

我将 spring-web-4.0.3.RELEASE 与 spring-boot-starter-web-1.0.2.RELEASE、jackson-databind-2.4.2 和 jackson-datatype-jsr310-2.4.2 一起使用

据我了解,Spring 应该自动为 Java8.time 对象注册 JSR-310 模块。

我找到了问题的答案,但它对我不起作用:Spring Boot and Jackson, JSR310 in response body

我没有使用 @EnableWebMvc 注释的配置,这是我唯一的配置类

@EnableAutoConfiguration
@ComponentScan
@EnableAspectJAutoProxy()
@Configuration
@ImportResource(value = "Beans.xml") 
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToLocalDateTime());
        registry.addConverter(new LocalDateTimeToString());
        registry.addConverter(new StringToLocalDate());
        registry.addConverter(new LocalDateToString());
        registry.addConverter(new StringToLocalTime());
        registry.addConverter(new LocalTimeToString());
    }
}

你能建议我的配置有什么问题吗?

【问题讨论】:

  • 人们花时间帮助您并回答问题。请花时间接受答案。

标签: json spring-mvc java-8 spring-boot java-time


【解决方案1】:

在 SpringBoot 1.5 中,我使用了以下依赖项并且开箱即用!

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

【讨论】:

    【解决方案2】:

    你的配置没有问题。

    我建议你将 Spring Boot 更新到 1.2.0.RELEASE。它使用 Spring 4.1.3 包,已解决 MappingJackson2HttpMessageConverter 的问题。要获取更多信息,请阅读:https://github.com/spring-projects/spring-boot/issues/1620#issuecomment-58016018。它在我的情况下有效。

    如果您不想更新 Spring Boot 版本,您可能至少可以像这样显式注释 LocalTime 的字段:

    @JsonSerialize(using = DateTimeSerializer.class)
    @JsonDeserialize(using = DateTimeDeserializer.class)
    private LocalTime date;
    

    【讨论】:

      【解决方案3】:

      您需要将模块注册为 Bean 以便 Spring 看到并设置它

      @Configuration
      public class JacksonConfiguration {
      
          @Bean
          public JSR310Module jsr310Module() {
              return new JSR310Module();
          }
      }
      

      【讨论】:

      • 如果您使用 SpringBoot,只需将 jar 添加到类路径(通过 Maven 或 Gradle)就足够了。 SpringBoot 会在类路径中看到它并自动使用它。
      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2013-03-04
      • 2018-07-19
      相关资源
      最近更新 更多