我正在使用带注释的Spring 3.1 MVC代码(spring-mvc),当我通过@RequestBody发送日期对象时,日期显示为数字.这是我的控制器
@Controller
@RequestMapping("/test")
public class MyController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
@RequestMapping(value = "/getdate", method = RequestMethod.GET)
public @ResponseBody Date getDate(@RequestParam("dt") Date dt, Model model) {
// dt is properly constructed here..
return new Date();
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过日期时,我能够以格式收到日期.但我的浏览器将日期显示为数字
1327682374011
Run Code Online (Sandbox Code Playgroud)
如何以我为webbinder注册的格式显示日期?我在一些论坛上看到我应该使用杰克逊映射器,但我不能改变现有的映射器?
我使用的是spring-boot,我有一个像这样定义的实体类
import org.joda.time.LocalDateTime;
@Entity
public class Project {
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime start_date;
...
...
}
Run Code Online (Sandbox Code Playgroud)
当此类转换为JSON时,该字段将转换为以下字符串表示形式
{"start_date":[2014,11,15,0,0,0,0],...., ...}
Run Code Online (Sandbox Code Playgroud)
我想让json响应为yyyy-MM-dd.
我尝试了@DateTimeFormat(iso = ISO.DATE)注释,但也没有帮助.
是否有一种简单的方法可以将此转换为正确的json格式?