【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错: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 类型成功。