Failed to convert from type [java.lang.String] to type [java.util.Date]

nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘2019-05-30 14:47:24’; nested exception is java.lang.IllegalArgumentException]]

今天做项目的时候遇到了一个BUG,springboot中后台接收前端传过来的时间报错,后面捣鼓了一阵发现前端传过来的是json数据,所以时间变为string类型的了,后台就转换不了咯。
Failed to convert from type [java.lang.String] to type [java.util.Date]_第1张图片
后台报错:
Failed to convert from type [java.lang.String] to type [java.util.Date]_第2张图片
在controller层加个@InitBinder注解可以解决,但是试了一下好像还是有点小BUG,就是在没有传时间的情况下会报错,前端不能传NULL过来,但是这不是我要得结果啊,SO可能是我打开的方式不对。。。。。
Failed to convert from type [java.lang.String] to type [java.util.Date]_第3张图片
对了,好像这个注解的方法不能为public。
不传值,也就是为NULL的情况下报错:
Failed to convert from type [java.lang.String] to type [java.util.Date]_第4张图片
最最后简单粗暴的方法,完美解决,就是要加的注解有点多,O(∩_∩)O哈哈~,不过有CV大法,SO小意思啦,就是在每个pojo的Date类型的字段加个@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)注解
Failed to convert from type [java.lang.String] to type [java.util.Date]_第5张图片
运行成功。
Failed to convert from type [java.lang.String] to type [java.util.Date]_第6张图片

你可能感兴趣的:(springboot)