You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The key point is that the @DateTimeFormat annotation only affects how Spring MVC binds incoming request parameters—it doesn’t change the serialization of dates in your generated Swagger documentation. To update the UI description for a LocalDate property, you need to control the serialization process that Swagger (via Jackson) uses.
There are two common approaches:
1. Annotate the Field:
Use the Jackson @jsonformat annotation along with the Swagger @Schema annotation. For example:
@Schema(type = "string", pattern = "dd/MM/yyyy", example = "01/02/2025") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") private LocalDate finalDate;
This tells Jackson to serialize the LocalDate using the "dd/MM/yyyy" pattern and informs Swagger of the intended format.
2. Configure the ObjectMapper Globally:
If you prefer a global solution (for all LocalDate fields), you can customize your Jackson ObjectMapper. In a Spring Boot application, for example, you can register a JavaTimeModule and set the date format:
@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.setDateFormat(new SimpleDateFormat("dd/MM/yyyy")); return mapper; }
This approach ensures that all LocalDate values are serialized in the desired format across your API documentation.
Using either approach will adjust the date format in the Swagger UI description from "yyyy-MM-dd" to "dd/MM/yyyy".
Hello.
I need to change the date format in the UI description from "yyyy-MM-dd" to "dd/MM/yyyy".
Could anyone help me?
UI Example:
"finalDate": "01/02/2025",
Not work:
@DateTimeFormat(pattern = "dd/MM/yyyy")
Java property:
@Parameter private LocalDate finalDate;
Swagger v3:
<dependency> <groupId>io.swagger.core.v3</groupId> <artifactId>swagger-annotations</artifactId> <version>2.2.21</version> </dependency>
The text was updated successfully, but these errors were encountered: