Datum als request-parameter in Spring REST

Mein controller sieht wie folgt aus:

@RequestMapping(value = "/process_date", method = RequestMethod.GET)
    public ResponseEntity processDate
       (@RequestParam(value = "time", required = false) 
        @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ssXXX") Date date){
//process the date
}

Den BRIEFTRÄGER-Abfrage:

http://localhost:8080/process_date?date=2014-05-09T00:48:16-04:00

Es mir IllegalArgumentException. Die volle Ausnahme ist:

{
  "timestamp": 1495736131978,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
  "message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2013-05-10T07:48:16-04:00'; nested exception is java.lang.IllegalArgumentException: Illegal pattern component: XXX",
  "path": "/airspaces"
}

Nun, seltsam, wenn ich ausführen:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
try {
    System.out.println(df.parse("2013-05-10T07:48:16-04:00"));
} catch (ParseException e) {
    System.out.println("PARSE EXCEPTION!!!");
}

Es funktioniert ohne Ausnahme. Dasselbe Datum-format, Gleiches Datum.

Ein workaround wäre, erhalten das Datum als string und dann die conversions durch die parser-Methode.

Aber ich bin besorgt, mehr über das, was hinter der Szene hier.

Es ist wahrscheinlich, weil-Konverter und-Formatierer sind 2 verschiedene Dinge. Haben Sie schon überprüft, dieser thread: forum.Frühling.io/forum/Feder-Projekte/web/... ?

InformationsquelleAutor Akeshwar Jha | 2017-05-25

Schreibe einen Kommentar