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+"============");
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM