问题是将JSON中的时间戳字段映射到java.sql.Timestamp时出现了错误。

huangapple 未分类评论70阅读模式
英文:

Issue with mapping timestamp field in json to java.sql.Timestamp

问题

我正在尝试将 JSON 输入传递给 Spring REST 端点。JSON 中有一个字段是:

"timestamp": "2017-04-12 10:12:31.4610"

在领域类中,相应的字段是:

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSX")
Timestamp timestamp;

但是我在处理该字段时遇到了反序列化问题:

HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.sql.Timestamp` from String "2017-04-12 10:12:31.4610": not a valid representation (error: Failed to parse Date value '2017-04-12 10:12:31.4610': Cannot parse date "2017-04-12 10:12:31.4610": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))

我还尝试了:

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSX")
Timestamp timestamp;

但是仍然抛出相同的错误。我在这里犯了什么错误?

英文:

I am trying to pass a json input to a spring rest endpoint. JSON has one of the field as:

"timestamp": "2017-04-12 10:12:31.4610"

In domain class, corresponding field is:

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSX")
Timestamp timestamp;

But I get deserialization issue with this field:

HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.sql.Timestamp` from String "2017-04-12 10:12:31.4610": not a valid representation (error: Failed to parse Date value '2017-04-12 10:12:31.4610': Cannot parse date "2017-04-12 10:12:31.4610": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))

I also tried with:

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSX")
Timestamp timestamp;

But same error is thrown.

What mistake am I making here?

huangapple
  • 本文由 发表于 2020年8月15日 02:39:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/63418384.html
匿名

发表评论

匿名网友

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

确定