【问题标题】:Springdoc webflux automatically convert String to java.time.LocalDate errorSpringdoc webflux自动将String转换为java.time.LocalDate报错
【发布时间】:2021-06-22 03:54:51
【问题描述】:

当我使用org.springdoc:springdoc-openapi-spring-boot-2-webflux:3.1.5构建响应式api时。

我收到以下错误:

Caused by: java.time.format.DateTimeParseException: Text '2021-03-24' could not be parsed at index 4

示例代码:

  • 控制器
@RestController
public class Controller {
  @GetMapping(path = "/test")
  public Mono<String> test(@Valid TaskCriteria taskCriteria) {
    Mono.just("Subscribe completed, criteria: " + taskCriteria.toString());
  }
}
  • 型号
@Data
@AllArgsConstructor
@Schema(description = "Task matching criteria", name = "TaskCriteria")
@ParameterObject
public class TaskCriteria {

  @Parameter(description = "Date to run, using ISO-8601 format yyyy-MM-dd")
  @Schema(required = true, type = "string", format = "date", example = "2021-03-25")
  @NotNull
  private LocalDate date;

}
  • Springboot 版本:2.3.2.RELEASE

【问题讨论】:

    标签: springdoc


    【解决方案1】:

    您的问题是您使用 @AllArgsConstructor 而不是 @NoArgsConstructor,对于 jackson!

    这是您的工作示例:

    @RestController
    public class HelloController {
    
        @GetMapping(path = "/test")
        public Mono<String> test(@Valid TaskCriteria taskCriteria) {
            return Mono.just("Subscribe completed, criteria: " + taskCriteria.toString());
        }
    
        @Data
        @NoArgsConstructor
        @Schema(description = "Task matching criteria", name = "TaskCriteria")
        @ParameterObject
        public class TaskCriteria {
    
            @Parameter(description = "Date to run, using ISO-8601 format yyyy-MM-dd")
            @Schema(required = true, type = "string", format = "date", example = "2021-03-25")
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
            @NotNull
            private LocalDate date;
    
        }
    
    }
    

    【讨论】:

    • 谢谢!这对我很有用。
    猜你喜欢
    • 2022-01-11
    • 2014-02-10
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多