0

In the Spring-boot project I try to pass as a request parameter a Date object and get this error:

Parameter value [1] did not match expected type [java.util.Date (n/a)]

This is the http-requset that i send:

http://localhost:8080/moneyManager/customer/actionBetweenDates?startDate=2019/07/01&endDate=2019/07/30

This is the fuction that trigger in the rest:

@RequestMapping(path="actionBetweenDates", method=RequestMethod.GET)
public Collection<Action> getActionByDate(@RequestParam Date startDate, @RequestParam Date endDate){
    return customerService.getAllActionBetweenDate(getSession().getId(), startDate, endDate);
}

The function in rest trigger the function in service :

public Collection<Action> getAllActionBetweenDate(long customerId, Date startDate, Date endDate) {
        Collection<MethodPayment> customerMethodPayments = methodPaymentRepository.findByCustomerId(customerId);
        Collection<Action> customerActionByDates = new ArrayList<>();
        for (MethodPayment mp : customerMethodPayments) {
            customerActionByDates
                    .addAll(actionRepository.findByDateBetweenAndMethodPaymentId(mp.getId(), startDate, endDate));
        }
        return customerActionByDates;
    }

The function in the service trigger the funcion in the repository:

Collection<Action> findByDateBetweenAndMethodPaymentId(long methodPaymentId, Date startDate, Date endDate);

What am I doing wrong?

Updating:

I finded the problem. the problem was related to the function found in actionRepository. The signature of the function first asks for two dates to compare between them and then id and I gave it opposite values. It's clear to me that after I got on it I would have had a problem with the date so the answers did help me. Thank you all!

Marko Previsic
  • 1,820
  • 16
  • 30
CM3194
  • 35
  • 5
  • Have you tried adding `@DateTimeFormat(pattern="yyyy/MM/dd")` – Ori Marko Jul 07 '19 at 07:40
  • 1
    Possible duplicate of [How to accept Date params in a GET request to Spring MVC Controller?](https://stackoverflow.com/questions/15164864/how-to-accept-date-params-in-a-get-request-to-spring-mvc-controller) – Avi Jul 07 '19 at 07:43
  • Avi, I work on spring boot. I never work in spring MVC controller and I do not know the way the question writer used – CM3194 Jul 07 '19 at 07:51

1 Answers1

1

Change your controller method to:

@RequestMapping(path="actionBetweenDates", method=RequestMethod.GET)
public Collection<Action> getActionByDate(@RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date startDate, @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date endDate){
    return customerService.getAllActionBetweenDate(getSession().getId(), startDate, endDate);
}

Check Annotation Type DateTimeFormat for detailed info, for examples of usages look Working with Date Parameters in Spring


UPD 1:
Adding sample @SpringBootApplication class and example request:

@SpringBootApplication
@RestController
public class DateProblemApp {

    public static void main(String[] args) {
        SpringApplication.run(DateProblemApp.class, args);
    }

    @RequestMapping(path="actionBetweenDates", method = RequestMethod.GET)
    public String getActionByDate(@RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date startDate, @RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd") Date endDate) {
        return "ok";
    }

}

Example request: http://localhost:8080/actionBetweenDates?startDate=2019/07/01&endDate=2019/07/30

Vüsal
  • 2,580
  • 1
  • 12
  • 31