Search code examples
spring-bootrestresttemplate

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


I've got 2 aplication one calling another. In the first one I have

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();

the url is defined as global http://ip:8090/connection/search When I'm looking at the request I can see that every parameter has it's proper value, but in the restTemplate.exchange line I got error "Parameter stationFrom is missing"

My endpoint on provided IP looks like :

@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

I don't know why it throws me this error if stationFrom has for example value 40 and I can see it in debug/logs

Of course calling provided endpoint from postman, with the same data gives me expected result :)


Solution

  • You are passing the query parameters (RequestParams) as headers.

    Check the following example how to pass 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());
            }
    }