1

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 :(

3
  • 1
    Show us the your post request. Looks like you are passing a badly formatted value for from/to. Commented Jul 23, 2018 at 13:57
  • 1
    Iso date format is YYYY-MM-DD not DD-MM-YYYY or MM-DD-YYYY. Your default values are in the wrong format. Also if it only is a date why use LocalDateTime over LocalDate? Commented Jul 23, 2018 at 13:58
  • I have no request, just spring taking default values - to avoid my wrong query in browser. I using LocalDateTime, because I have DATETIME in database Commented Jul 23, 2018 at 14:03

1 Answer 1

4

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) {
Sign up to request clarification or add additional context in comments.

1 Comment

Say that you provide a pattern of for example@DateTimeFormat(pattern="dd-MM-yyyy-hh-mm-ss"), you would not need to change the type to LocalDate correct? After adjusting the pattern I am still receiving the same error. How would you deal with this if you wish to go by LocalDateTime?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.