Spring Boot

Springboot Date类型日期参数报错的解决办法


在使用springboot 即 springMVC中,绑定接口传递时间字符串数据给Date类型报如下错误:

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 
-Failed to bind request element: 
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: 
Failed to convert value of type [java.lang.String] to required 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 '1990-05-31 00:00:00';
nested exception is java.lang.IllegalArgumentException

原因是:Springboot 并不会跟Struts2一样将Date类型字符串自动转换为Date类型,so 就需要我们自己去进行转换,解决办法如下:

@InitBinder
protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

 

Spring Boot
idea使用spring boot 热更新、热加载
Spring Boot
Springboot中logback配置日志按天分割
Spring Boot
Spring Boot 非web应用程序实例(控制台应用程序)
还没有评论哦,快来抢沙发吧!