如何在Spring MVC Controller的GET请求中接受Date参数? [英] How to accept Date params in a GET request to Spring MVC Controller?

查看:1347
本文介绍了如何在Spring MVC Controller的GET请求中接受Date参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GET请求,将YYYY-MM-DD格式的日期发送到Spring控制器。
控制器代码如下:

  @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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆