我有一个控制器方法看起来像这样:
@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
}
我的对象长这样:
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将这个日期解析成那个日历对象!(虽然我更喜欢它是一个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']