java @DateTimeFormat(pattern="yyyy-MM-dd") 与 Spring Mvc Rest Service 给出“错误 400 请求在语法上不正确”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30163144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@DateTimeFormat(pattern="yyyy-MM-dd") with Spring Mvc Rest Service gives "error 400 request syntactically incorrect"
提问by Paolof76
I'm not able to let this work:
我不能让这个工作:
@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) {
with the request:
随着请求:
http://localhost:8080/rest/people?birthDay=2014-05-25
The error is always :
错误总是:
"HTTP Status 400 - The request sent by the client was syntactically incorrect".
“HTTP 状态 400 - 客户端发送的请求在语法上不正确”。
I can't find any resource that give me a clear answer/guide to understand the underlying problem... could someone help me? Thanks!
我找不到任何资源可以给我一个明确的答案/指南来理解潜在的问题......有人可以帮助我吗?谢谢!
Edit:Exception is:
编辑:例外是:
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
回答by borjab
In the spring forum theysaid that you need to have a conversion service initialized in your configuration in order to use the Formatter automátically. Something like:
在 spring 论坛中,他们说您需要在配置中初始化转换服务才能自动使用格式化程序。就像是:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
</bean>
</property>
</bean>
回答by Paolof76
Finally I found the solution, maybe could be of use for others with the same issue. Just add this method to the 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));
}
right in the documentation: http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-webdatabinder
就在文档中:http: //docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-webdatabinder
But still don't understand why @DateTimeFormat(pattern="yyyy-MM-dd") doesn't work... With this solution there is no need of DateTimeFormat, so I annotated the param like the others:
但仍然不明白为什么@DateTimeFormat(pattern="yyyy-MM-dd") 不起作用......使用这个解决方案不需要DateTimeFormat,所以我像其他人一样注释了参数:
@RequestParam(required = false) Date birthDay