当前位置:   article > 正文

解决前后端传参日期转换异常错误——JSON parse error: Cannot deserialize value of type `java.util.Date` from String

json parse error: cannot deserialize value of type `java.util.date` from str

完整错误提示为:

[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"])]
  • 1
  • 2

该问题在于前台传参的时间是字符串并且带有时分秒,而后台实体接收的是date日期类型,所以导致JSON异常转换错误。

解决方法如下:

在实体类的时间字段上方添加以下转换注解:

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

在这里插入图片描述
注意:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/896723
推荐阅读
相关标签
  

闽ICP备14008679号