【spring boot】传递时间类型的字符串,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

而POST请求,传入时间类型字符串,后台是可以解析成Date类型的。

出现这个错误,在需要接受Date类型的字符串参数的controller层中,加入:

@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

重启,再次进行访问,即可GET请求传入时间字符串,后台解析为Date类型成功。

更多:

https://www.cnblogs.com/sxdcgaq8080/p/10399458.html

https://blog.csdn.net/gwd1154978352/article/details/75041733