Skip to content

Default databinding for java 8 LocalDate #5523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anthony-foulfoin opened this issue Mar 29, 2016 · 12 comments
Closed

Default databinding for java 8 LocalDate #5523

anthony-foulfoin opened this issue Mar 29, 2016 · 12 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@anthony-foulfoin
Copy link

Given an endpoint with a LocalDate parameter :

public void doSth(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate) {
 ...
}

I must set @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) everywhere I want to bind a LocalDate. This code is boilerplate so I defined a custom editor :

webDataBinder.registerCustomEditor(LocalDate.class, new LocalDateEditor());

But it would be great if spring boot was able to do this natively with a date format in the properties file, and ISO as default.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 29, 2016
@philwebb
Copy link
Member

DateTimeFormatterRegistrar has some options setters that we might be able to surface as properties.

@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 29, 2016
@snekse
Copy link

snekse commented Sep 9, 2016

What are the pros/cons of using WebDataBinder#registerCustomEditor vs FormatterRegistry#addConverter

@unlimitedsola
Copy link
Contributor

+1, its painful to spam @DateTimeFormat everywhere

@ghost
Copy link

ghost commented Apr 19, 2017

This should also work by default in POJOs for GET request. It does work for POST.

@xriva
Copy link

xriva commented Jun 27, 2017

Vote up for this enhancement. Actually I feel somewhat surprised that there is no global configuration to change the default @DateTimeFormat.

@britter
Copy link
Contributor

britter commented Aug 1, 2017

What are the pros/cons of using WebDataBinder#registerCustomEditor vs. FormatterRegistry#addConverter

I only tried the WebDataBinder approach. This had the problem, that Optional<LocalDate> did no longer work for parameters with @RequestParam(required = false). It works when using @DateTimeFormat.

britter added a commit to britter/spring-boot that referenced this issue Aug 1, 2017
….LocalDate.

Initial attempt and basis for discussions on how to fix spring-projectsgh-5523.
@britter
Copy link
Contributor

britter commented Aug 2, 2017

I found a way to make it work even with 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);
      }
    };
  }

I tried to generalize this using the spring.mvc.date-pattern property and auto configuration in #9930.

britter added a commit to britter/spring-boot that referenced this issue Aug 2, 2017
….LocalDate.

Initial attempt and basis for discussions on how to fix spring-projectsgh-5523.
@britter
Copy link
Contributor

britter commented Aug 7, 2017

I've blogged about this: https://blog.codecentric.de/en/2017/08/parsing-of-localdate-query-parameters-in-spring-boot/

@bclozel bclozel self-assigned this Dec 22, 2017
@bclozel bclozel added this to the 2.0.0.RC1 milestone Dec 22, 2017
philwebb added a commit that referenced this issue Dec 22, 2017
Add an optional dependency to `javax.money` to Eclipse errors in
`WebConversionService`.

See gh-5523
See gh-11402
@jo-ka
Copy link

jo-ka commented Jul 31, 2018

I just tried to use this new feature with Spring Boot 2.0.1 and ran into parsing problems that I described in this Stack Overflow answer. Is this intended behaviour, have I been doing something wrong? My expectation was that yyyy-MM-dd would work just fine.

@wilkinsona
Copy link
Member

@jo-ka I think you've identified a bug. Can you please open a new issue? I believe the key difference is the use of ResolverStyle.STRICT when a custom format is specified. If no custom format is specified, the DataTimeFormatter is created using java.time.format.DateTimeFormatter.ofLocalizedDate(FormatStyle) which uses ResolverStyle.SMART. I suspect that inconsistency is accidental and, with confirmation from @bclozel, should be corrected.

@jo-ka
Copy link

jo-ka commented Aug 1, 2018

@wilkinsona Okay, I'll open a new issue , but only after @bclozel confirmed this, right?

@wilkinsona
Copy link
Member

Please open a new one now. If we have to close it because @bclozel disagrees with my analysis then that's better than it getting forgotten because it's only being tracked by this already-closed issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
10 participants