在 Spring boot 项目中无法使用日期请求参数调用 GET API

问题描述 投票:0回答:1

我尝试使用 LocalDateTime 格式调用 GET API,但它不断给出请求参数丢失的错误。

这是我要调用的API

@RequestMapping(value = "/api", method = RequestMethod.GET) public void apiCall(@RequestParam("restrictProfileDate") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime restrictProfileDate) {

这是我用来调用此 API 的代码

        HttpHeaders headers = new HttpHeaders();   
    LocalDate deltascreeningDate = LocalDate.now();
    Date date = Date.from(dateValue.atStartOfDay(ZoneId.systemDefault()).toInstant()); 

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = sdf.format(date);

    Map<String, String> params = new HashMap<>();
    params.put("restrictProfileDate", formattedDate);
    
    ResponseEntity<String> response = restTemplate.exchange(apiEndPoint, HttpMethod.GET, new HttpEntity<>(params, headers), String.class);

我收到的错误是“警告 o.s.w.s.m.s.DefaultHandlerExceptionResolver - 已解决 [org.springframework.web.bind.MissingServletRequestParameterException:所需的 LocalDateTime 参数‘restrictProfileDate’不存在]”

java spring-boot date
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.