如何在Spring MVC Controller的GET请求中接受Date参数? [英] How to accept Date params in a GET request to Spring MVC Controller?
问题描述
控制器代码如下:
@RequestMapping(value =/ fetch,method = RequestMethod.GET )
public @ResponseBody String fetchResult(@RequestParam(from)Date fromDate){
//内容到这里
}
我正在使用Firebug进行检查时正确发送请求。
我得到错误:
HTTP状态400:客户端发送的请求在语法上不正确。
如何让控制器接受Date格式?
请帮忙。我做错了什么?
好的,我解决了。
为任何可能在一整天的不间断编码和累加之后累了的人写下来错过这样一个愚蠢的事情。
@RequestMapping(value =/ fetch,method = RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam(from)@DateTimeFormat(pattern =yyyy-MM-dd)Date fromDate){
//内容到这里
}
是的,很简单。只需添加DateTimeFormat注释。
I've a GET request that sends a date in YYYY-MM-DD format to a Spring Controller. The controller code is as follows:
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") Date fromDate) {
//Content goes here
}
The request is sent correctly as I'm checking with Firebug. I get the error:
HTTP Status 400: The request sent by the client was syntactically incorrect.
How can I make the controller accept this format of Date? Please help. What am I doing wrong?
Ok, I solved it. Writing it for anyone who might be tired after a full day of non-stop coding & miss such a silly thing.
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") @DateTimeFormat(pattern="yyyy-MM-dd") Date fromDate) {
//Content goes here
}
Yes, it's simple. Just add the DateTimeFormat annotation.
这篇关于如何在Spring MVC Controller的GET请求中接受Date参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!