JsonDeserialize对于LocalDateTime不起作用。

huangapple go评论138阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年8月27日 18:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63614407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定