Skip to content
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

@RequestHeader doesn't accept RFC-1123 for conversion to java.time.Instant [SPR-14201] #18775

Closed
spring-issuemaster opened this issue Apr 21, 2016 · 2 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Apr 21, 2016

James Howe opened SPR-14201 and commented

For example, I'm trying to receive an HTTP date header:

@RequestHeader(HttpHeaders.IF_MODIFIED_SINCE) Optional<Instant> ifModifiedSince

but this results in a conversion error

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [java.util.Optional]
	at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:115)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:99)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
    ... 88 more
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestHeader java.time.Instant] for value 'Thu, 21 Apr 2016 17:11:08 +0100'
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192)
	at org.springframework.core.convert.support.ObjectToOptionalConverter.convert(ObjectToOptionalConverter.java:75)
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:173)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:108)
	at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)
	at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)
	at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:688)
	at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:107)
	... 91 more
Caused by: java.time.format.DateTimeParseException: Text 'Thu, 21 Apr 2016 17:11:08 +0100' could not be parsed at index 0
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.Instant.parse(Instant.java:395)
	at org.springframework.format.datetime.standard.InstantFormatter.parse(InstantFormatter.java:40)
	at org.springframework.format.datetime.standard.InstantFormatter.parse(InstantFormatter.java:35)
	at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:194)
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
	... 101 more

I tried to add @DateTimeFormat but it is unwieldy - the only built-ins are ISO-8601 formats. It's not possible to specify Java 8's DataTimeFormatter.RFC_1123_DATE_TIME to this annotation.

Given that all HTTP date/time headers should be using the RFC 1123 format, shouldn't that be the default for @RequestHeader conversion? There should at least be better formatter integration with java.time classes.

I'm running in a Spring Boot 1.3.3 context, if that makes any difference.


Affects: 4.2.5

Issue Links:

  • #18143 @DateTimeFormat's JSR-310 formatter is not strict in case of pattern
  • #18922 Support locale parameter to DateTimeFormat annotation

Referenced from: commits 9412f7a

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 28, 2016

Juergen Hoeller commented

This seems to work fine for java.util.Date arguments... The problem is rather that our InstantConverter simply delegates to Instant.parse(String); we might have to make this a bit smarter by default. For the time being, you'd have to register your own converter for java.time.Instant with the MVC ConversionService, overriding Spring's default one.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 28, 2016

Juergen Hoeller commented

Since standard java.util.Date parsing is equally lenient there, I've added explicit support for RFC-1123 conversion to our InstantFormatter, making this available not only to @RequestHeader but to any conversion from String to Instant. We simply 'smartly' adapt to the ISO Instant format as well as to RFC-1123 now. This will be available in 4.3 RC2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.