[英]spring boot offsetDateTime in query param
I try to create the following api:我尝试创建以下 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);
}
But when I call it I get the following error:但是当我调用它时,出现以下错误:
{
"_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": [
]
}
I found solutions for localDateTime but they don't seem to work for offsetDateTime,我找到了 localDateTime 的解决方案,但它们似乎不适用于 offsetDateTime,
You need to add the @DateTimeFormat annotation for the OffsetDateTime arguments in the controller. eg.您需要为 controller 中的 OffsetDateTime arguments 添加@DateTimeFormat注释。例如。
@GetMapping("/workerslist")
public List<Worker> getWorkersList(
@RequestParam(name = "lastPollBefore", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)OffsetDateTime lastPollBefore,
@RequestParam(name = "lastPollAfter", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)OffsetDateTime lastPollAfter){
return workerService.getWorkers(lastPollBefore, lastPollAfter);
}
And you may want to override the Spring's default Object Mapper by your own custom mapper.您可能希望通过您自己的自定义映射器覆盖 Spring 的默认 Object 映射器。
Something like this:是这样的:
@Configuration
public class JsonConfiguration {
private static final ObjectMapper OBJECT_MAPPER =
new ObjectMapper().registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
@Bean
@Primary
public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}
}
I try to create the following api:我尝试创建以下 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);
}
But when I call it I get the following error:但是当我调用它时,我收到以下错误:
{
"_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": [
]
}
I found solutions for localDateTime but they don't seem to work for offsetDateTime,我找到了 localDateTime 的解决方案,但它们似乎不适用于 offsetDateTime,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.