Adam
Adam

Reputation: 161

Spring Boot -Failed to convert from type [java.lang.String] to type RequestParam

in my controller I have an endpoint:

  @GetMapping(value = SUMMARY_URL, produces = "application/json")
    public DailyReportSummary getSummaryOfDailyReports(
            @RequestParam(name = "from", required = false,defaultValue = "10-10-2017 ") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDateTime from,
            @RequestParam(name = "to", required = false,defaultValue = "10-10-2019 ") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDateTime to) {

        List<DailyReport> summary = statisticService.findByDateToSummary(from, to);

        DailyReportSummary dailyReportSummary = new DailyReportSummary(summary);

I though that all is ok, but I have this error:

   There was an unexpected error (type=Bad Request, status=400).
    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 '10-10-2017 '; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [10-10-2017 ]

What is wrong with this? I trying this solve this, but nothing works.

edit: with deleted iso still errors :(

Upvotes: 1

Views: 10829

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44942

Since the pattern dd-MM-yyyy doesn't have time part you need to use LocalDate

@GetMapping(value = SUMMARY_URL, produces = "application/json")
public DailyReportSummary getSummaryOfDailyReports(
        @RequestParam(name = "from", required = false, defaultValue = "10-10-2017") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate from,
        @RequestParam(name = "to", required = false, defaultValue = "10-10-2019") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate to) {

Upvotes: 4

Related Questions