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

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':
 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))
 at [Source: (PushbackInputStream); line: 9, column: 15]

解决办法:

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

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

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

Logo

一站式虚拟内容创作平台,激发创意,赋能创作,进入R空间,遇见同道,让优质作品闪耀发光。​

更多推荐