QUESTION: Spring appears to use different deserialization methods for LocalDate
depending on whether it appears in a @RequestBody
or a request @R
Create a Formatter for LocalDate:
public class LocalDateFormatter implements Formatter {
@Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, DateTimeFormatter.ISO_DATE);
}
@Override
public String print(LocalDate object, Locale locale) {
return DateTimeFormatter.ISO_DATE.format(object);
}
}
Spring 5+:
Register the formatter:
Implement WebMvcConfigurer
in your @Configuration
and override addFormatters
:
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new LocalDateFormatter());
}
Spring Boot: Define a @Primary @Bean
to override the default formatter:
@Bean
@Primary
public Formatter localDateFormatter() {
return new LocalDateFormatter();
}