Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to globally configure `@DateTimeFormat` pattern in Spring Boot?

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?

like image 675
britter Avatar asked Aug 01 '17 14:08

britter


People also ask

How do I extract data between two dates in spring boot?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

Does spring boot require XML configuration to function?

xml file is required! When required, however, we can take control over parts of the configuration and override the conventions that Spring Boot puts in play. We can also, if we really must, use traditional XML configuration files for some parts of the configuration.

What is the use of @document in spring boot?

Annotation @Document The annotations @Document applied to a class marks this class as a candidate for mapping to the database. The most relevant parameter is value to specify the collection name in the database. The annotation @Document specifies the collection type to DOCUMENT .


1 Answers

This is currently not easily possible (e.g. by setting a simple configuration property), see #5523. The best solution I found so far is to register a Formatter<LocalDate>. This will also work with optional parameters modeled as Optional<LocalDate>:

  @Bean
  public Formatter<LocalDate> localDateFormatter() {
    return new Formatter<LocalDate>() {
      @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);
      }
    };
  }

It may become possible to set this using a configuration property when my proposal in #9930 has been merged.

like image 53
britter Avatar answered Oct 06 '22 05:10

britter