首页 > 解决方案 > 时间戳反序列化失败并显示“无法将'java.lang.String'类型的值转换为所需的'java.time.Instant'类型”

问题描述

我希望有人能指出我在这里缺少的东西。

我有一个带有休息控制器的 Spring Boot 2 应用程序,它接受一些请求参数,其中一个是 java.time.Instant 作为“@RequestParam("offset") Instant offset”。我希望这可以从基于秒或毫秒的时间戳中反序列化,例如 1604303073000。

然而我总是得到提到的异常,起源于 org.springframework.format.datetime.standard.InstantFormatter.parse(InstantFormatter.java:50) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]

将 Instant 序列化为基于秒或毫秒的时间戳可以很好地使用这些参数:

jackson:
  serialization:
    write-dates-as-timestamps: true
    write-date-timestamps-as-nanoseconds: true
  deserialization:
    read-date-timestamps-as-nanoseconds: false

但相反的方式似乎对反序列化完全没有影响,无论它设置为真还是假。Spring 显然只是试图将我的时间戳应用于错误的格式化程序。

我能找到的所有类似问题似乎都暗示这是一个不费吹灰之力的默认行为,但这已经导致了一些令人沮丧的数小时的反复试验。

标签: javajsonspring-bootjacksondeserialization

解决方案


推荐阅读