Java 如何让 DateTimeFormat 与 Spring MVC 一起工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23251895/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:31:11  来源:igfitidea点击:

How do I get DateTimeFormat working with Spring MVC?

javaspringspring-mvc

提问by Trant

I have a controller method that looks like this:

我有一个看起来像这样的控制器方法:

@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 looks like this:

MyOBject 看起来像这样:

public class MyObject{

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Calendar someDate;
    // blah blah setters getters

When I call this method via browser like so:

当我通过浏览器调用这个方法时:

http://localhost:8080/blah/test?someDate=2011-07-11T21%3A28%3A59.564%2B01%3A00

I am getting Error 400 - Bad Request.

我收到错误 400 - 错误的请求。

I kept trying with various different values for someDate (always using a URL encoder to encode the special chars) and nothing works. All the ones I tried (pre-URL encoding):

我一直在尝试使用 someDate 的各种不同值(总是使用 URL 编码器对特殊字符进行编码),但没有任何效果。我尝试过的所有(前 URL 编码):

2000-10-31 01:30:00.000-05:00

2000-10-31 01:30:00.000-05:00

2011-07-11T21:28:59.564+01:00

2011-07-11T21:28:59.564+01:00

2014-04-23T13:49:28.600Z

2014-04-23T13:49:28.600Z

I know dates dont match I am merely trying to get Spring to parse this damn date for me into that Calendar object!! (Though really I would prefer it was a java.sql.Timestamp but that's probably even harder to get done)

我知道日期不匹配我只是想让 Spring 为我解析这个该死的日期到那个 Calendar 对象中!!(虽然我真的更喜欢它是一个 java.sql.Timestamp 但这可能更难完成)

How do I do this?

我该怎么做呢?

Am I writing the date wrong? Am I using the wrong annotation for properties in a ModelAttribute (Note I have many other parameters so I bundle them up in a ModelAttribute object, dont want to use @RequestParm)

我写错日期了吗?我是否对 ModelAttribute 中的属性使用了错误的注释(注意我有许多其他参数,所以我将它们捆绑在 ModelAttribute 对象中,不想使用 @RequestParm)

The error that shows up in log file:

日志文件中显示的错误:

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']

采纳答案by Sotirios Delimanolis

This

这个

'2011-07-11T21:28:59.564+01:00'

value is incorrect since the expected format is

值不正确,因为预期的格式是

yyyy-MM-dd'T'HH:mm:ss.SSSZ

you can't have a :inside the +0100timezone offset.

你不能:+0100时区偏移内有一个。

You must be url encoding it wrong.

你一定是 url 编码错误。