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
Error message is self-explanatory:
there is no registered String
to Instant
converter.
When Controller
receives request, all parameters are String
s. 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?