英文:
JsonDeserialize not work for LocalDateTime
问题
我想从我的客户端向后端发送POST请求,在POJO中我有两个字段LocalDate和LocalDateTime,如下所示:
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy - hh:mm:ss")
private LocalDateTime createdTimestamp;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate expiredDate;
客户端将发送带有以下主体的请求:
{
"expiredDate" : "01.01.2020",
"createdTimestamp" : "01.02.2020 - 10:10:10"
}
然而,在后端,我遇到了异常:
java.lang.NoSuchMethodError:
com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;
如果我将请求中的createdTimestamp
字段省略,则一切正常。似乎只有注释@JsonDeserialize(using = LocalDateDeserializer.class)
起作用,而@JsonDeserialize(using = LocalDateTimeDeserializer.class)
未起作用。
是否有人知道为什么会发生这种情况?
英文:
I would like to send POST request from my client to my backend, and in the POJO I have two fields LocalDate and LocalDateTime as follows:
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy - hh:mm:ss")
private LocalDateTime createdTimestamp;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy")
private LocalDate expiredDate;
The client will send request with body like:
{
"expiredDate" : "01.01.2020",
"createdTimestamp" : "01.02.2020 - 10:10:10"
}
In the backend, however, I got an exception:
java.lang.NoSuchMethodError:
com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;
And if I leave the createdTimestamp
out of request's body then it worked. It seems that only the annotation @JsonDeserialize(using = LocalDateDeserializer.class)
worked, while the @JsonDeserialize(using = LocalDateTimeDeserializer.class)
did not work.
Does anyone have an idea why this happened?
答案1
得分: 3
你在模式中使用了12小时制的小时符号(hh
),但是模式不完整,因为缺少了表示AM
/PM
的指示器,这将是一个a
。保留该指示器会使时间表达不明确,它可能是上午10点
或下午10点
(24小时制中的22
)。
你可以将格式切换为24小时制(HH
),这将使时间变得明确无误。
像这样:
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy - HH:mm:ss")
private LocalDateTime createdTimestamp;
英文:
You are using the symbol for hours in 12h format (hh
) in your pattern, but the pattern is incomplete because it is missing an indicator for AM
/PM
, which would be an a
. Leaving that indicator makes the time expression ambiguous, it could be 10 AM
or 10 PM
(22
in 24h-format).
You could just switch the format to 24h format (HH
), which would make the time unambiguous.
Like this:
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy - HH:mm:ss")
private LocalDateTime createdTimestamp;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论