JSON parse error: Can not deserialize value of type java.sql.Timestamp from String

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_35959554/article/details/88312138
JSON parse error: Can not deserialize value of type java.sql.Timestampfrom String "2019-03-22 00:00:00":
 not a valid representation (error: Failed to parse Date value '2019-03-22 00:00:00':
 Can not parse date "2019-03-22 00:00:00":
 while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
 Can not deserialize value of type java.sql.Timestamp from String "2019-03-22 00:00:00":
 not a valid representation (error: Failed to parse Date value '2019-03-22 00:00:00':
 Can not parse date "2019-03-22 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))

使用soring boot 的时候,移动端post json数据中带了Timestamp(Data)数据,并且服务端实体类是这两种时间格式的话,如果不做任何操作的话那么就会发生上异常。

原因:

jackson转换成Timestamp(Data)时,默认数据的时间格式是 'yyyy-MM-dd'T'HH:mm:ss.SSS’,但是请求格式可能为 'yyyy-MM-dd HH:mm:ss',格式不同所以转换失败。

解决:

在实体类Timestamp(Data)数据上设置你要转换的格式比如

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",  timezone="GMT+8")

 timezone="GMT+8":保证时区统一

猜你喜欢

转载自blog.csdn.net/weixin_35959554/article/details/88312138