2

I'm trying to write a Spring MVC GET controller that takes a Java 8 Instant as request parameter:

    @GetMapping
    @JsonView(OrderListing.class)
    @Validated
    public WebSeekPaginatedResultsDto<OrderListingDto> findAll(@Valid OrderSearchCommand orderSearchCommand) {
       // Some code
    }

with:

    public class OrderSearchCommand {
        private Instant dateCreatedStart;
        private Instant dateCreatedEnd;
        // Some other fields
    }

I'm triggering a GET request from some React/Javascript code with something like that:

    http://localhost:8080/mms/front/orders?dateCreatedStart=2017-05-31T22%3A00%3A00.000Z 

Spring does not seem to like it and throws an error. Here is the error message:

    Failed to convert property value of type 'java.lang.String' to required type 'java.time.Instant' for property 'dateCreatedStart'; 
    nested exception is java.lang.IllegalStateException: 
    Cannot convert value of type 'java.lang.String' to required type 'java.time.Instant' for property 'dateCreatedStart': no matching editors or conversion strategy found

Any idea why I'm getting this?

Thank you

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user2490936
  • 129
  • 2
  • 14
  • The parameter you are receiving is of type `String` and you are trying to store it in an `Instant`. You must convert `String` to `Instant`. – luizfzs Jun 30 '17 at 17:14
  • 1
    You must annotate the fields with `@DateTimeFormat` as shown in the [duplicate link](https://stackoverflow.com/q/40940615/5221149). – Andreas Jun 30 '17 at 17:16
  • If `Instant` does not work with the "duplicate" post, take a look at section 9.5 in the spring reference (https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#core-convert) . Section 9.5.5 covers registration of your custom converter. – DwB Jun 30 '17 at 17:25
  • @Andreas I reopened the question because, I believe that, `@DateTimeFormat` does not work with `Instant`. `Instant` is not listed in the supported types for this annotation in the Spring 4 reference document. – DwB Jun 30 '17 at 17:31
  • 1
    @DwB Ahh, I just looked at javadoc of [`DateTimeFormat`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html), which said *"JSR-310 `java.time` types"*, but you are right, the source code of [`Jsr310DateTimeFormatAnnotationFormatterFactory`](https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java#L51) only lists 6 types: LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime, OffsetTime. – Andreas Jun 30 '17 at 20:33
  • Thank you all for your answers Actually I've already tried `@DateTimeFormat` but it didn't work, it doesn't work with `Instant` type I'll check the converter and see how it will go Thank you – user2490936 Jun 30 '17 at 21:54

3 Answers3

3

Error message is self-explanatory: there is no registered String to Instant converter.

When Controller receives request, all parameters are Strings. Spring/Jackson has list of predefined converter for most of basic types: - String > Integer - String > Boolean

But there is no default String > Instant converter.

You need to create and register one. Or you can change input type to something that Spring can handle with @DateTimeFormat annotation: How to accept Date params in a GET request to Spring MVC Controller?

A. Tim
  • 3,046
  • 1
  • 13
  • 15
0

I suspect that the @DateTimeFormat will not work for Instant fields. Specifically, Instant is not listed as one of the types for which it works in the Spring reference doc.

Check out Spring Reference Section 9.5 for details about Spring custom type conversion. Create a Converter<String, Instant> converter. Section 9.5.5 covers custom type converter registration.

DwB
  • 37,124
  • 11
  • 56
  • 82
0

Maybe this can help you? serialize/deserialize java 8 java.time with Jackson JSON mapper I see that this Jackson include Instant class.

Spasoje Petronijević
  • 1,476
  • 3
  • 13
  • 27