Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Swagger 3 UI display example date in DDMMYYYY format?

I have a Spring Boot app with REST API, and in one of the request object, I declared a field which is supposed to hold a date in the format 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;

In the Swagger UI, the example value of this field in the request body is always shown as below (current date in the format YYYY-MM-DD).

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

I have this in my 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>

How can I make the Swagger UI display the example date in the format DDMMYYYY? As you can see in my codes above, I put in @Parameter and @Schema but I really don't know how they work.

like image 837
user3573403 Avatar asked Sep 16 '25 03:09

user3573403


1 Answers

The problem lies in the format. The format is only valid if we use the English format. if we want to use dd/mm/yyyy we can't put format in the annotation. for this reason we must use the annotation without the format and it has worked for me using localDate

@Schema(type = "string", pattern = "dd-MM-yyyy", example = "17-02-2020")
private LocalDate fecha;
like image 84
unknow Avatar answered Sep 19 '25 13:09

unknow