下面的示例适用于POST请求,但不适用于GET。我的目标是让spring从LocalDate输入(请求体和查询参数)自动解析String:
@PostMapping("/datetime")
public Mono<String> datetimePost(@RequestBody DateTimeDto dto) {
return Mono.just("OK");
}
static class DateTimeDto {
private LocalDate date;
//getter, setter
}作品: POST localhost:8080/datetime
{
"date": "2022-02-02"
}但与GET不一样的是:
获取本地主机:8080/datetime?日期=2022-02-02
@GetMapping("/datetime")
public Mono<String> datetimeGet(DateTimeDto dto) {
return Mono.just("OK);
}结果:
{
"timestamp": "2022-03-03T08:57:56.248+00:00",
"status": 400,
"message": "'date': : Parse attempt failed for value [2022-02-02]. rejectedValue: 2022-02-02"
}方法中索引0处的参数验证失败:带有1错误的公共org.springframework.web.bind.support.WebExchangeBindException: org.example.spring.webflux.ExampleServlet.datetimeGet(org.example.spring.webflux.ExampleServlet$DateTimeDto),:[字段‘date’上的对象'dateTimeDto‘中的字段错误:拒绝值2022-02-02;代码typeMismatch.dateTimeDto.date、typeMismatch.date、typeMismatch.java.time.LocalDate、typeMismatch;参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码dateTimeDto.date,日期;参数[];默认消息日期];默认消息[未能将' java.lang.String‘类型的属性值转换为属性’date‘所需的类型' java.time.LocalDate’;嵌套异常是java.lang.String未能将值‘2022-02-02’从java.lang.String类型转换为java.time.LocalDate类型;嵌套异常是org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.lambda$null$3(ModelAttributeMethodArgumentResolver.java:139) ~spring-webflux-5.3.16.jar:5.3.16上的java.lang.IllegalArgumentException:对值2022-02-02]的解析尝试失败]
发布于 2022-03-03 10:04:23
因此,由于GET + POST的工作方式不同,下面至少解决了这个问题:
application.properties
spring.webflux.format.date=ISO
spring.webflux.format.time=ISO
spring.webflux.format.date-time=ISOhttps://stackoverflow.com/questions/71334501
复制相似问题