'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.
Solution 1:[1]
You can use ApiModelProperty to specify the swagger default/example value.
@ApiModelProperty(value = "dd/MM/yyyy", example = "20/02/2022")
private LocalDate creationDate;
Or
@ApiModelProperty(value = "ddMMyyyy", example = "20022022")
protected LocalDate creationDate;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zakaria Hossain |
