我的控制器的請求參數如下:日期爲彈簧安置
@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
}
郵遞員查詢:
http://localhost:8080/process_date?date=2014-05-09T00:48:16-04:00
它給我IllegalArgumentException
。完整的例外是:
{
"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"
}
現在,奇怪的是,當我運行:
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!!!");
}
它的工作沒有任何異常。相同的日期格式,相同的日期。
一種解決方法是將日期作爲字符串接收,然後通過解析器方法執行轉換。
但是我更關心這裏幕後發生了什麼。
這可能是因爲轉換器和格式化程序是兩個不同的東西。你已經檢查過這個線程:http://forum.spring.io/forum/spring-projects/web/86986-datetimeformat-for-pathvariable-not-converting? – cristianorbs