gpt4 book ai didi

java - 参数值 [1] 与预期类型不匹配

转载 作者:行者123 更新时间:2023-12-02 09:42:54 27 4
gpt4 key购买 nike

在 Spring-boot 项目中,我尝试将 Date 对象作为请求参数传递,并收到此错误:

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

这是我发送的http请求:

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

这是其余部分触发的功能:

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

休息中的函数触发服务中的函数:

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;
}

服务中的函数触发存储库中的函数:

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

我做错了什么?

更新:

我发现了问题。该问题与 actionRepository 中找到的函数有关。该函数的签名首先要求两个日期进行比较,然后 id 和我给它相反的值。我很清楚,在我上了它之后,我会对日期产生疑问,所以答案确实对我有帮助。谢谢大家!

最佳答案

将 Controller 方法更改为:

@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);
}

检查Annotation Type DateTimeFormat有关详细信息,有关用法示例,请查看 Working with Date Parameters in Spring

<小时/>

UPD 1:
添加示例 @SpringBootApplication 类和示例请求:

@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";
}

}

示例请求:http://localhost:8080/actionBetweenDates?startDate=2019/07/01&endDate=2019/07/30

关于java - 参数值 [1] 与预期类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56920199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com