【发布时间】:2020-10-21 21:45:00
【问题描述】:
我尝试创建以下 api:
@GetMapping("/workerslist")
public List<Worker> getWorkersList(
@RequestParam(name = "lastPollBefore", required = false) OffsetDateTime lastPollBefore,
@RequestParam(name = "lastPollAfter", required = false) OffsetDateTime lastPollAfter){
return workerService.getWorkers(lastPollBefore, lastPollAfter);
}
但是当我调用它时,我收到以下错误:
{
"_type": "ValidationError",
"key": "validation-error",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.OffsetDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.OffsetDateTime] for value '2020-07-01T11:52:57.152Z'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-07-01T11:52:57.152Z]",
"constraints": [
]
}
我找到了 localDateTime 的解决方案,但它们似乎不适用于 offsetDateTime,
【问题讨论】:
-
“我找到了 localDateTime 的解决方案” 比如什么?你是说像这个? How to use LocalDateTime RequestParam in Spring? I get “Failed to convert String to LocalDateTime” --- 您是否尝试添加依赖项
jackson-datatype-jsr310?您是否尝试将@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)添加到参数中?还是@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX")?