【问题标题】:How to make Swagger 3 UI display example date in DDMMYYYY format?如何使 Swagger 3 UI 以 DDMMYYYY 格式显示示例日期?
【发布时间】:2022-06-24 05:53:42
【问题描述】:

我有一个带有 REST API 的 Spring Boot 应用程序,在其中一个请求对象中,我声明了一个字段,该字段应该以 DDMMYYYY 格式保存日期:

@Parameter(required = true, example = "20022022")
@Schema(required = true, type = "date", format = "ddmmyyyy", example = "20022022")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "ddMMyyyy")a
@JsonDeserialize(using = LocalDateDeserializer.class)
@NotNull
private LocalDate valueDate;

在 Swagger UI 中,请求正文中该字段的示例值始终如下所示(当前日期,格式为 YYYY-MM-DD)。

{
    ...
    "valueDate": "2022-03-17"
}

我的 pom.xml 中有这个:

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-webmvc-core</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.6</version>
    </dependency>

如何让 Swagger UI 以 DDMMYYYY 格式显示示例日期?正如您在上面的代码中看到的,我输入了@Parameter 和@Schema,但我真的不知道它们是如何工作的。

【问题讨论】:

  • 您找到解决方案了吗?也使用 Swagger 3,我尝试了 @Schema 但这不起作用

标签: spring-boot swagger swagger-ui openapi


【解决方案1】:

您可以使用ApiModelProperty 指定swagger 默认/示例值。

@ApiModelProperty(value = "dd/MM/yyyy", example = "20/02/2022")
private LocalDate creationDate;

或者

@ApiModelProperty(value = "ddMMyyyy", example = "20022022")
protected LocalDate creationDate;

【讨论】:

  • 我正在使用 Swagger 3。ApiModelProperty 来自 Swagger 2。我尝试过 @Schema 但它不起作用。
【解决方案2】:

Swagger github 建议将 example = "19680228" 添加到您的 @Schema 注释中,但是,这对我不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多