我有一个控制器方法,如下所示:
@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
}
MyOBject看起来像这样:
public class MyObject{
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Calendar someDate;
// blah blah setters getters
当我通过浏览器调用此方法时:
http://localhost:8080/blah/test?someDate=2011-07-11T21%3A28%3A59.564%2B01%3A00
我收到错误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']
答案 0 :(得分:8)
此
'2011-07-11T21:28:59.564+01:00'
值不正确,因为预期格式为
yyyy-MM-dd'T'HH:mm:ss.SSSZ
您在:
时区偏移内无+0100
。
你必须将其编码错误。