解决日期转换异常 JSON parse error: Cannot deserialize value of type `java.util.Date` from String

r囧r小猫 2022-11-26 12:57 308阅读 0赞

记录一下解决Spring 传参时,日期 date类型,前端传入日期字符串【带有时分秒】,bean类接收JSON转换异常问题,异常详情如下:

  1. Cannot deserialize value of type `java.util.Date` from String "2020-08-12 11:45:12": not a valid representation (error: Failed to parse Date value '2020-08-12 11:45:12':
  2. Cannot parse date "2020-08-12 11:45:12": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
  3. at [Source: (PushbackInputStream); line: 9, column: 15]

解决办法:

在对应的bean日期属性上增加注解 @JsonFormat 注解

  1. @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
  2. private Date followTime;

转换默认格式为:年月日,如果传入的日期字符串没有包含 时分秒,则不需要增加注解,pattern 为当前接收的时间格式

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读