ZonedDateTime as PathVariable in Spring REST RequestMapping
我在Spring应用程序中有一个REST端点,看起来像这样
@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吗?
还是传递为字符串然后自己进行转换的唯一方法?如果是这样,是否有一种通用方法可以处理具有类似设计的多个方法?