完美解决报错Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

时间:2023-03-08 17:10:16

Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

首先这个错误的意思是 前台页面传递在string类型的时间数据,比如'2020-05-31 10:00:00' 后台使用Date类型去接收,但是报错了。

解决方法:

要解决这个问题其实很简单, 在接收的字段上面,添加下面的注解 就可以了

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //返回时间类型

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") //接收时间类型

private Date startTime;

注解解释:

@DateTimeFormat

该注解自动会解析处理,会把字符串类型 按照格式yyyy-MM-dd HH:mm:ss 转换成时间类型

@JsonFormat

这个注解是spring里面controller返回到页面的的转换. 把时间类型 转换成JSON格式类型,

前提取出进行展示.