Parameter value [1] did not match expected type

非 Y 不嫁゛ 提交于 2019-12-13 03:23:34

问题


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!


回答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



来源:https://stackoverflow.com/questions/56920199/parameter-value-1-did-not-match-expected-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!