I have a problem when I try to use RequestParam with a LocalDateTime:
public ResponseData getTotalRevenue(@RequestParam(value = "startDate")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "endDate")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate){
//....
}
I test with this params :
/total-revenue?startDate=2022-07-28&endDate=2022-08-20
I get the following error:
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; 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.LocalDateTime]
for value '2022-07-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2022-07-28]]
Anyone can help me? Thank you so much.
CodePudding user response:
DateTimeFormat.ISO.DATE_TIME is a format that expects date and time, but your request just includes the date. Try DateTimeFormat.ISO.DATE instead.
CodePudding user response:
The error is self-explanatory:
Parse attempt failed for value [2022-07-28]]
I think you want your startDate
and endDate
to be of type LocalDate
and not LocalDateTime
because you want to represent dates.
Anyway, you are passing the query parameters in wrong DateTimeFormat
. Instead of DateTimeFormat.ISO.DATE_TIME
, you should use DateTimeFormat.ISO.DATE
.
public ResponseData getTotalRevenue(@RequestParam(value = "startDate")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime startDate,
@RequestParam(value = "endDate")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime endDate) {
//....
}
Here is an example for your reference.
Look at the documentation for all the ISO formats.