赞
踩
完整错误提示为:
[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2020-12-31 10:46:17": not a valid representation (error: Failed to parse Date value '2020-12-31 10:46:17': Cannot parse date "2020-12-31 10:46:17": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-12-31 10:46:17": not a valid representation (error: Failed to parse Date value '2020-12-31 10:46:17': Cannot parse date "2020-12-31 10:46:17": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
at [Source: (PushbackInputStream); line: 1, column: 100] (through reference chain: com.cd.project.model.Approval["projectStartTime"])]
该问题在于前台传参的时间是字符串并且带有时分秒,而后台实体接收的是date日期类型,所以导致JSON异常转换错误。
解决方法如下:
在实体类的时间字段上方添加以下转换注解:
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
注意:
转换默认格式为:-年-月-日,如果传入的日期字符串没有包含-时-分-秒,则不需要添加该注解。
参数:pattern 为当前接收的时间格式。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。