【发布时间】:2021-05-22 04:14:51
【问题描述】:
我正在使用 Java 11,Spring Boot 2.2.6.RELEASE。如何将“2019-10-21T13:00:00+02:00”反序列化为 LocalDateTime?
到目前为止尝试过:
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@DateTimeFormat(iso = ISO.DATE_TIME)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
private LocalDateTime startTime;
但我收到以下错误:
2021-02-19 07:45:41.402 WARN 801117 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2019-10-21T13:00:00+02:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2019-10-21T13:00:00+02:00' could not be parsed, unparsed text found at index 19; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2019-10-21T13:00:00+02:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2019-10-21T13:00:00+02:00' could not be parsed, unparsed text found at index 19
at [Source: (PushbackInputStream); line: 2, column: 18] (through reference chain: example.app.dto.DtoRequest["startTime"])]
【问题讨论】:
-
显示的值包含时区偏移量,应映射到
OffsetDateTime,而不是LocalDateTime。 -
@RoarS。 - 你提到的重复目标没有回答这个问题。详情请查看下方答案。
标签: java json spring-boot java-time