RestTemplate.exchange 无法正常工作。抛出缺少的参数

RestTemplate.exchange doesn't work properly. Missing params thrown

我有 2 个应用程序,一个调用另一个。第一个我有

RestTemplate restTemplate = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
                MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
                parameters.add("stationFrom", stationfrom);
                parameters.add("stationTo", stationto);
                parameters.add("operator", 5);
                parameters.add("dateTimeFrom", sdfnative.format(sdfeskm.parse(departuredate)));
                parameters.add("dateTimeTo", sdfnative.format(sdfeskm.parse(departuredate).getTime() + 60*60*1000));
                HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(parameters, headers);
                jsonTrain = restTemplate.exchange(url, HttpMethod.GET, request, String.class).toString();

url定义为全局http://ip:8090/connection/search 当我查看请求时,我可以看到每个参数都有正确的值,但在 restTemplate.exchange 行中我收到错误“Parameter stationFrom is missing”

我在提供的 IP 上的端点如下所示:

@GetMapping(value = "/connection/search")
public ResponseEntity<String> getConnection(@RequestParam(value = "stationFrom") int stationFrom,
                                @RequestParam(value = "stationTo") int stationTo,
                                @RequestParam(value = "operator") int operator,
                                @RequestParam(value = "dateTimeFrom") String dateTimeFrom,
                                @RequestParam(value = "dateTimeTo") String dateTimeTo) throws JSONException

我不知道为什么如果 stationFrom 有例如值 40 时它会抛出这个错误,我可以在 debug/logs

中看到它

当然,使用相同的数据调用邮递员提供的端点会得到预期的结果:)

您正在将查询参数 (RequestParams) 作为 headers 传递。

检查以下示例如何传递 RequestParams:

public void findUserById() 
{
        String username = "chathuranga";
        String password = "123";
        Integer userId = 1;

        String url = "http://localhost:" + port + "/users/" + userId;

        //setting up the HTTP Basic Authentication header value
        String authorizationHeader = "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());

        HttpHeaders requestHeaders = new HttpHeaders();
        //set up HTTP Basic Authentication Header
        requestHeaders.add("Authorization", authorizationHeader);
        requestHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE);

        //request entity is created with request headers
        HttpEntity<AddUserRequest> requestEntity = new HttpEntity<>(requestHeaders);

        //adding the query params to the URL
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("name", "chathuranga")
                .queryParam("email", "chathuranga.t@gmail.com");

        ResponseEntity<FindUserResponse> responseEntity = restTemplate.exchange(
                uriBuilder.toUriString(),
                HttpMethod.GET,
                requestEntity,
                FindUserResponse.class
        );

        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            System.out.println("response received");
            System.out.println(responseEntity.getBody());
        } else {
            System.out.println("error occurred");
            System.out.println(responseEntity.getStatusCode());
        }
}