查询参数中的java spring boot offsetDateTime
我尝试创建以下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
# 1 楼答案
您需要为控制器中的OffsetDateTime参数添加@DateTimeFormat注释。例如
您可能想用自己的自定义映射器覆盖Spring的默认对象映射器
比如: