Spring-JSON返回日期格式
首先是类型转换问题!
在JavaBean里字段是Date类型,对应数据库的也是Date类型,但后台和前台交互的时候,前台客户端传给后台的都是一个字符串,这时怎么办!!格式化没错,但是可以用自动格式化
//指定一个birthday进行绑定@InitBinder public void initBinder(WebDataBinder binder) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, "birthday", new CustomDateEditor(format, true)); }//对所有的Date类型进行绑定@InitBinder public void initBinder(WebDataBinder binder) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); binder.registerCustomEditor(Date.class,new CustomDateEditor(format, true)); }