-1

I am trying to query my database by passing a date param as path variable on postman using a get mapping, however i keep getting this error "I am trying to query my database by passing a date param as path variable on postman using a get mapping, however I keep getting this error

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'; 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.sql.Date] for value '2019-07-24 11:50:34.896+01'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.Date] to type [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.sql.Date]]

I have tried using the @DateTimeFormat as suggested by various people on line but it still isn't working.

@GetMapping("/walletledger/{fromDate}/{toDate}/{currencycode}")
@PreAuthorize("hasAuthority('SUPERADMIN')")
public List < LedgerResponseDTO > fetchWalletLedgers(
    @PathVariable("fromDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date fromDate,
    @PathVariable("toDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date toDate
}

Implementation

@Override
public List < LedgerResponseDTO > fetchWalletLedgers(
        Date fromDate,
        Date toDate,
        CurrencyCode currencyCode,
        Pageable pageable) {
        List < LedgerResponseDTO > ledgerList = new ArrayList < > ();
        ledgerRepository.findByExecutionDateAndExecutionDateAndCurrencyCode(fromDate, toDate, currencyCode).forEach(ledger - > {
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77

1 Answers1

2
@GetMapping(value = "/date/{from}/{to}")
    public String DemoDate(@PathVariable(name = "from") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date from,
            @PathVariable(name = "to") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date to)
    {
        return from.toString() + " "+ to.toString();
    }

URL to be called

localhost:8888/date/2019-07-25/2019-07-26

DateTimeFormat.ISO

Postman

Romil Patel
  • 12,879
  • 7
  • 47
  • 76