Springboot请求日期类型参数报错:org.springframework.validation.BeanPropertyBindingResult: 1 errors

Springboot 或SpringMVC Controller层请求参数包含Date类型,传入参数为“yyyy-MM-dd HH:mm:ss”格式的日期,报错如下:

org.springframework.validation.BeanPropertyBindingResult: 1 errors

解决方法:

在Springboot 或SpringMVC Controller层添加如下代码即可:

@InitBinder
public void init(WebDataBinder binder) {
    binder.registerCustomEditor(Date.class,
            new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
}

 @InitBinder是给Binder做初始化的,被此注解的方法可以对WebDataBinder初始化。webDataBinder是用于表单到方法的数据绑定的,@InitBinder只在@Controller中注解方法来为这个控制器注册一个绑定器初始化方法,方法只对本控制器有效。 
 


版权声明:本文为angellee1988原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。