SpringBoot 调用报错:Required request parameter ‘endDate‘ for method parameter type Date is not present

问题复现

@GetMapping("/dateAddFormatters")
public String dateAddFormatters(@RequestParam("startDate") Date date1, @RequestParam("endDate") Date date2) {
    // addFormatters里配置的处理日期逻辑
    System.out.println(date1);
    System.out.println(date2);
    return "success";
}

调用:http://localhost:8080/dateAddFormatters?startDate=2023-05-17

***ResponsePostInterceptor.preHandle***
***GlobalExceptionHandler.handleException:Required request parameter 'endDate' for method parameter type Date is not present***
***ResponsePostInterceptor.afterCompletion***

原因

@RequestParam("endDate") 默认为 @RequestParam(value = "endDate", required = true)

方案

方案1:传递endDate参数:http://localhost:8080/dateAddFormatters?startDate=2023-05-17&?endDate=2023-05-19
方案2:endDate改为非必须参数: @RequestParam(value = "endDate", required = false)

@GetMapping("/dateAddFormatters")
public String dateAddFormatters(@RequestParam("startDate") Date date1, @RequestParam(value = "endDate", required = false) Date date2) {
    // addFormatters里配置的处理日期逻辑
    System.out.println(date1);
    System.out.println(date2);
    return "success";
}

SpringBoot 调用报错:Required request parameter ‘endDate‘ for method parameter type Date is not present_第1张图片

***ResponsePostInterceptor.preHandle***
Wed May 17 08:00:00 CST 2023
null
***ResponsePostInterceptor.postHandle***
***ResponsePostInterceptor.afterCompletion***

你可能感兴趣的:(常见问题,spring,boot,后端,java)