1

My Controller Mapping

@GetMapping("/fetch/{one_date}/{two_date}")
public List<CourierInfo> getData_between(@PathVariable(value = "one_date") @DateTimeFormat(pattern = "yyyyMMdd") LocalDateTime fromDate, @PathVariable(value = "two_date") @DateTimeFormat(pattern = "yyyyMMdd") LocalDateTime toDate) {
    return bookRepository.getData_between(fromDate, toDate);
}

My Custom Query

@Query(nativeQuery = true, value="select c.cons_no, c.pick_date, from CourierInfo c where c.pick_date between :startDate and :endDate")

List getData_between(@Param("startDate") LocalDateTime date, @Param("endDate") LocalDateTime date2);

I am passing

http://localhost:8080/book_api/fetch/2020-01-20/2020-01-20

Here I am trying to fetch data between two dates. I am getting this 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.PathVariable @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '"2020-01-20"'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value ["2020-01-20"]
Mayank S. Gupta
  • 69
  • 1
  • 11

2 Answers2

1

First of all stop using java.util.Date and start using LocalDate from java-8 date time API, you can parse the input date string into LocalDate using DateTimeFormatter

@GetMapping("/fetch/{one_date}/{two_date}")
public List<CourierInfo> getData_between(@PathVariable(value = "one_date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate fromDate, @PathVariable(value = "two_date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate toDate) {
    return bookRepository.getData_between(fromDate, toDate);
}

And in the repository

@Query(nativeQuery = true, value="select c.cons_no, c.pick_date, from CourierInfo c where c.pick_date between :startDate and :endDate")
List<CourierInfo> getData_between(@Param("startDate") LocalDate date, @Param("endDate") LocalDate date2);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • http://localhost:8080/book_api/fetch/2020-01-20/2020-01-23 passed this still error – Mayank S. Gupta Jan 23 '20 at 11:47
  • Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; this is error I am getting – Mayank S. Gupta Jan 23 '20 at 11:47
  • can you update your post with request and stack trace @MayankS.Gupta – Ryuzaki L Jan 23 '20 at 11:48
  • Notice that the pattern `yyyyMMdd` accepts `20200120`, not `2020-01-20`. I'd use `@DateTimeFormat(iso=ISO.DATE)` for the sake of standards. – Luis Iñesta Jan 23 '20 at 11:55
  • Since you have only date just use `LocalDate` and make sure the input request date format should match `DateTimeFormatter` @MayankS.Gupta, try the updated code – Ryuzaki L Jan 23 '20 at 11:58
  • you are not at all trying anything, read my comment first @MayankS.Gupta the input date string should match the pattern in `DateTimeFormatter` annotation – Ryuzaki L Jan 23 '20 at 14:06
0

Check this topic : https://stackoverflow.com/a/53188501/4135813

The problem is related to missing converter in your endpoint configuration