我尝试使用 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’不存在]”
new HttpEntity<>(params, headers)
实际上使用第一个参数作为主体,而不是查询参数。 您应该在
String.class
之后传递参数