Tra*_*ant 4 java spring spring-mvc
我有一个控制器方法,如下所示:
@RequestMapping(headers = "Accept=application/json;charset=utf-8", value = "/test", method = RequestMethod.GET)
@ResponseBody
public Blah test(@ModelAttribute MyObject parms, HttpServletRequest request) throws Exception {
// blah blah
}
Run Code Online (Sandbox Code Playgroud)
MyOBject看起来像这样:
public class MyObject{
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Calendar someDate;
// blah blah setters getters
Run Code Online (Sandbox Code Playgroud)
当我通过浏览器调用此方法时:
http://localhost:8080/blah/test?someDate=2011-07-11T21%3A28%3A59.564%2B01%3A00
Run Code Online (Sandbox Code Playgroud)
我收到错误400 - 错误请求.
我一直在尝试使用someDate的各种不同值(总是使用URL编码器来编码特殊字符)并且没有任何作用.我试过的所有(URL前编码):
2000-10-31 01:30:00.000-05:00
2011-07-11T21:28:59.564 + 01:00
2014-04-23T13:49:28.600Z
我知道日期不匹配我只是想让Spring将这个该死的日期解析为我的Calendar对象!! (虽然我真的希望它是一个java.sql.Timestamp,但这可能更难完成)
我该怎么做呢?
我写错了日期吗?我在ModelAttribute中对属性使用了错误的注释(注意我有很多其他参数,所以我将它们捆绑在ModelAttribute对象中,不想使用@RequestParm)
日志文件中显示的错误:
Field error in object 'myObject' on field 'someDate': rejected value [2011-07-11T21:28:59.564+01:00]; codes [typeMismatch.myObject.someDate,typeMismatch.someDate,typeMismatch.java.util.Calendar,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [myObject.someDate,someDate]; arguments []; default message [someDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Calendar' for property 'someDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Calendar for value '2011-07-11T21:28:59.564+01:00'; nested exception is java.lang.IllegalArgumentException: Unable to parse '2011-07-11T21:28:59.564+01:00']
Run Code Online (Sandbox Code Playgroud)
这个
'2011-07-11T21:28:59.564+01:00'
Run Code Online (Sandbox Code Playgroud)
值是不正确的,因为预期的格式是
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Run Code Online (Sandbox Code Playgroud)
你不能: 在+0100时区偏移内.
你必须将url编码错误.
| 归档时间: |
|
| 查看次数: |
10248 次 |
| 最近记录: |