【发布时间】:2020-06-18 12:11:56
【问题描述】:
有这样的课
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public final class ActiveRecoveryProcess {
private UUID recoveryId;
private Instant startedAt;
}
我收到com.fasterxml.jackson.databind.exc.InvalidFormatException 和消息Cannot deserialize value of typejava.time.Instantfrom String "2020-02-22T16:37:23": Failed to deserialize java.time.Instant: (java.time.format.DateTimeParseException) Text '2020-02-22T16:37:23' could not be parsed at index 19
JSON 输入
{"startedAt": "2020-02-22T16:37:23", "recoveryId": "6f6ee3e5-51c7-496a-b845-1c647a64021e"}
杰克逊配置
@Autowired
void configureObjectMapper(final ObjectMapper mapper) {
mapper.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
mapper.findAndRegisterModules();
}
编辑
JSON 是从 postgres 生成的
jsonb_build_object(
'recoveryId', r.recovery_id,
'startedAt', r.started_at
)
r.started_at 是 TIMESTAMP。
【问题讨论】:
-
Instant正在整个项目中使用。为什么我应该考虑使用LocalDateTime而不是Instant? -
如果传入的数据只是说
2020-02-22T16:37:23,而最后没有Z,你怎么知道确定时间是UTC?也许使用LocalDateTime更适合这种没有时区的时间值。 -
我编辑了我的帖子 - JSON 是从 Postgres 生成的,
jsonb_build_object()函数
标签: java json spring-boot jackson