Im my Spring Boot application, I have some controllers that accept a date as query parameter:
@RestController
public class MyController {
@GetMapping
public ResponseEntity<?> getDataByDate(
@RequestParam(value = "date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
final LocalDate date) {
return ResponseEntity.ok();
}
}
This works well, and I can even mark the parameter as optional using @RequestParam(value = "date", required = false)
and then use an Optional<LocalDate>
. Spring will handle all this and pass an empty Optional when the parameter is missing.
Since I have several controllers using dates as query parameters, I want to configure this behavior for all LocalDate
query parameters. I have tried the spring.mvc.date-pattern
property, but it only seems to work for java.util.Date
.
So after searching the web, the best I came up with is a ControllerAdvice
I adopted from this answer. The problem with this solution is, that is can not handle Optional<LocalDate>
anymore. It feels like this is the wrong way to configure the behavior in Spring Boot.
So my question is: How do I globally configure the pattern for LocalDate
used as query parameters in an idiomatic way in Spring Boot?