Reputation: 779
I wanted to send request like this:
I have an error:
"2018-05-31 15:40:29.623 WARN 11496 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value '2018-03-22'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-03-22] "
The problem is obvious so I found the solution and I changed everything. My method:
@RequestMapping(path = "reports/daily", method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE)
public String getDailyReport(@RequestParam ("start_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDate startDate,
@RequestParam("end_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDate endDate) {
double totalDistance = 0.0;
double totalPrice = 0.0;
List<Transit> transits = transitService.getTransits(startDate, endDate);
for (Transit transit : transits) {
if (transit.getDistance() != null && transit.getPrice() != null) {
try {
totalDistance = totalDistance + transit.getDistance();
totalPrice = totalPrice + transit.getPrice();
} catch (NullPointerException e) {
e.fillInStackTrace().getMessage();
}
}
}
return "Total distance " + totalDistance + ", total price: " + totalPrice;
}
For me everything seems fine, I also added needed dependecies to pom file.
Upvotes: 3
Views: 9475
Reputation: 86296
I suspect the issue is here:
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
According to the docs DATE_TIME
is for date and time:
The most common ISO DateTime Format
yyyy-MM-dd'T'HH:mm:ss.SSSZ
, e.g. "2000-10-31T01:30:00.000-05:00".
Your parameter of 2018-03-22
doesn’t look this way. Instead use
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
The most common ISO Date Format
yyyy-MM-dd
, e.g. "2000-10-31".
The same for start_date
and end_date
.
Upvotes: 7