Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get DateTimeFormat working with Spring MVC?

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:

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.

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):

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

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

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)

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)

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']
like image 721
Trant Avatar asked Apr 23 '14 18:04

Trant


People also ask

Can you explain work flow of Spring MVC?

Understanding the flow of Spring Web MVC As displayed in the figure, all the incoming request is intercepted by the DispatcherServlet that works as the front controller. The DispatcherServlet gets an entry of handler mapping from the XML file and forwards the request to the controller.

Can we use spring boot and Spring MVC together?

Yes, you can use Spring MVC with Spring Boot. To create and run a Spring MVC web application in spring boot, you need to add the spring-boot-starter dependency in your pom. xml file.


1 Answers

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 +0100 timezone offset.

You must be url encoding it wrong.

like image 123
Sotirios Delimanolis Avatar answered Oct 07 '22 00:10

Sotirios Delimanolis