我有一个GET请求,它将YYYY-MM-DD格式的日期发送到Spring控制器。控制器代码如下:
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") Date fromDate) {
//Content goes here
}
当我检查Firebug时,请求被正确发送。我得到了错误:
HTTP状态400:客户端发送的请求语法错误。
如何让控制器接受这种格式的日期?请帮帮忙。我做错了什么?
发布于 2013-03-02 03:03:20
好了,我解决了。写给那些在一整天不停的编码后可能感到疲倦的人&怀念这样一件愚蠢的事情。
@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from") @DateTimeFormat(pattern="yyyy-MM-dd") Date fromDate) {
//Content goes here
}
是的,这很简单。只需添加DateTimeFormat注释即可。
发布于 2017-09-04 11:05:37
这就是我从前端获取格式化日期的方法
@RequestMapping(value = "/{dateString}", method = RequestMethod.GET)
@ResponseBody
public HttpStatus getSomething(@PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) String dateString) {
return OK;
}
你可以用它来得到你想要的东西。
发布于 2019-03-07 05:54:14
..。或者你可以用正确的方式来做这件事,并且有一个连贯的规则来序列化/反序列化整个应用程序中的日期。将此代码放入application.properties:
spring.mvc.date-format=yyyy-MM-dd
https://stackoverflow.com/questions/15164864
复制相似问题