SpringMVC:提交日期类型报400错误解决方法

方法1:可以使用@ControllerAdvice增强Controller

@ControllerAdvice
public class BaseControllerAdvice {
    // 初始化绑定
    @InitBinder
    public void initBinder(WebDataBinder binder) {
         //处理表单数据转换对象异常(Date)
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
}

方法2:直接实体类中的字段上加上@DateTimeFormat(pattern="yyyy-MM-dd")

@MappedSuperclass // 配置后,子类可以使用注解
public class BaseEntity implements Serializable {
@Column(name
= "CREATE_TIME") @Temporal(TemporalType.TIMESTAMP) // 实体类会封装成完整的时间“yyyy-MM-dd hh:MM:ss”的Date类型。 @DateTimeFormat(pattern="yyyy-MM-dd") private Date creteTime; // 创建时间
}

方法3:直接设置日期数据注解

@RequestMapping("/testDate")
public void testDate(@DateTimeFormat(pattern="yyyy-MM-dd") Date mydate){
    System.out.println(mydate+"============");
}
原文地址:https://www.cnblogs.com/guo-rong/p/9521676.html