我有一个 GET 请求,它将 YYYY-MM-DD 格式的日期发送到 Spring Controller。 Controller 代码如下:
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") Date fromDate) {
//Content goes here
}
在我使用 Firebug 进行检查时,请求已正确发送。 我得到了错误:
HTTP Status 400: The request sent by the client was syntactically incorrect.
如何让 Controller 接受这种日期格式? 请帮忙。我做错了什么?
最佳答案
好的,我解决了。 写给那些在一天不间断的编码之后可能会感到疲倦并错过这么愚蠢的事情的人。
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") @DateTimeFormat(pattern="yyyy-MM-dd") Date fromDate) {
//Content goes here
}
是的,很简单。只需添加 DateTimeFormat 注释即可。
关于java - 如何在对 Spring MVC Controller 的 GET 请求中接受日期参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164864/