我在我的Spring應用程序REST端點,看起來像這樣ZonedDateTime如PathVariable在彈簧安置RequestMapping
@RequestMapping(value="/customer/device/startDate/{startDate}/endDate/{endDate}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public Page<DeviceInfo> getDeviceListForCustomerBetweenDates(@PathVariable ZonedDateTime startDate, @PathVariable ZonedDateTime endDate, Pageable pageable) {
... code here ...
}
我試圖傳遞路徑變量既是毫秒和秒。不過,我得到以下異常兩種方式:
"Failed to convert value of type 'java.lang.String' to required type 'java.time.ZonedDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.PathVariable java.time.ZonedDateTime for value '1446361200'; nested exception is java.time.format.DateTimeParseException: Text '1446361200' could not be parsed at index 10"
是否有人可以解釋我是如何可以傳遞(無論是作爲秒或毫秒)的字符串,如1446361200,並把它轉換爲ZonedDateTime?
或者是作爲String傳遞的唯一方法,然後自己進行轉換?如果是的話,是否有一種通用的方法來處理類似設計的多種方法?
'ZonedDateTime'是相對較新的,並且Spring可能尚未更新以直接支持它。請參閱JasonZ的回答,尋找解決方法。 – Powerlord