java - @DateTimeFormat(pattern ="yyyy-MM-dd") 使用 Spring Mvc Rest Service 给出 "error 400 request syntactically incorrect"

标签 java json rest spring-mvc

<分区>

我无法让它工作:

@RequestMapping(value = "/people", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody
    List<IPerson> searchPerson(@RequestParam(value = "birthDay", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date birthDay) {

请求:

http://localhost:8080/rest/people?birthDay=2014-05-25

错误总是:

"HTTP Status 400 - The request sent by the client was syntactically incorrect".

我找不到任何资源可以给我一个明确的答案/指南来理解潜在的问题...有人可以帮助我吗?谢谢!

编辑: 异常(exception)是:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '2010-10-10'; nested exception is java.lang.IllegalArgumentException
    at org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:78)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:168)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:161)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:93)
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:61)

... 40 more
Caused by: java.lang.IllegalArgumentException
    at java.util.Date.parse(Date.java:615)
    at java.util.Date.<init>(Date.java:272)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:73)
    ... 45 more

最佳答案

最后我找到了解决方案,也许对有同样问题的其他人有用。只需将此方法添加到 Controller :

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

就在文档中:http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-webdatabinder

但仍然不明白为什么 @DateTimeFormat(pattern="yyyy-MM-dd") 不起作用...有了这个解决方案就不需要 DateTimeFormat,所以我像其他人一样注释了参数:

@RequestParam(required = false) Date birthDay

关于java - @DateTimeFormat(pattern ="yyyy-MM-dd") 使用 Spring Mvc Rest Service 给出 "error 400 request syntactically incorrect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163144/

相关文章:

java - 为自定义驱动程序创建 GraphicsDevice

java - 如何递归地使用比较器?

java - unity firebase 数据库检索数据

javascript - 基于值/对的 Handlebars.js JSON 设置类

json - 为 HTTP 响应在 JSON 中嵌入 XML 的最佳实践?

rest - 如何使用 Mutex 字段创建结构的元素

java - 将实数存储为 int 的接收/接受方法是什么?

ios - Swift 4 对成员 'jsonObject(with:options:)' 的引用不明确

javascript - 使用 .getjson 和 .each 构建变量后如何从回调返回变量

jquery - 从rest接口(interface)获取JSON [Object对象]