Announcement Announcement Module
Collapse
No announcement yet.
Spring cant convert String to Date on Server, local tests pass Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring cant convert String to Date on Server, local tests pass

    Hello,
    I do have some controllers reading the conditional get header form an request.
    In detail the "If-Modified-Since" header is required.
    My Unit tests looks as follows:

    Code:
    // setup date
    		Date currentDate = mu2.getLastModified();
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(currentDate);
    		calendar.add(Calendar.DATE, 1);
    		//Sat, 12 Aug 1995 13:30:00 GMT+0000
    		//EEE, dd.MM.yyyy HH:mm:ss Z
    		SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zZ");
    		formatter.setTimeZone((TimeZone.getTimeZone("GMT")));
    		
    		// setup request
    		request.setMethod("GET");
    		request.setRequestURI("/customer");
    		request.addHeader("Accept", "application/json");
    		request.addHeader("If-Modified-Since", formatter.format((calendar.getTime())));
    So local all tests are fine and pass. On the Jenkins I'll get an Exception:

    org.springframework.beans.ConversionNotSupportedEx ception: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date]: no matching editors or conversion strategy found

    How is that possible?

    Local everything allright, on the jenkins thos errors?

    I would appreciate any help
    Last edited by Alex04; Oct 27th, 2011, 09:28 AM.

  • #2
    Really nobody?

    It seems that spring is not able to convert the string dates for the method:
    Code:
    @RequestMapping(value = "/date={date}", method = RequestMethod.GET, headers = {"Accept=application/json", "If-Modified-Since", "Authorization"})
    	@ResponseBody
    	public ResponseEntity<String> showJsonForDateAndCustomer(@PathVariable("date") Long pDate, @RequestHeader("If-Modified-Since") @DateTimeFormat(pattern="dd.MM.yyyy HH:mm:ss z Z") Date lastModified)

    Comment


    • #3
      Hit this myself with the Spring 3.2.0 changes in this area, having been using

      @RequestHeader("If-Modified-Since") Date lastModified

      without any problems.

      The z format element (for GMT BST PDT etc) is only available as a formatter, not a parser in JodaTime 1.6.2. Hence if I put in @DateTimeFormat(pattern="EEE, dd MMM yyyy HH:mm:ss z"), I get an error parsing the "GMT" of my date string

      Comment

      Working...
      X