RestTemplate GET request with request params
Asked Answered
D

2

12

I have to call a REST webservice and I am planning to use RestTemplate. I looked at examples on how to make a GET request and they are as shown below.

 String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42","21");

In my case the RESTful url is something like below. How do I use RestTemplate in this case?

http://example.com/hotels?state=NY&country=USA

So my question would be how do I send request parameters for GET requests?

Dravidian answered 26/8, 2011 at 2:30 Comment(0)
H
35

the placeholders work the same for either type of url, just do

 String result = restTemplate.getForObject("http://example.com/hotels?state={state}&country={country}", String.class,"NY","USA");

or better yet, use a hashmap for real name matching-

Helping answered 14/10, 2011 at 21:55 Comment(0)
D
1

While making a request to a RESTful server, it requires in many a cases to send query parameters, request body (in case of POST and PUT request methods), as well as headers in the request to the server.

In such cases, the URI string can be built using UriComponentsBuilder.build(), encoded using UriComponents.encode() if needed to, and sent using RestTemplate.exchange() like this:

public ResponseEntity<String> requestRestServerWithGetMethod()
{
    HttpEntity<?> entity = new HttpEntity<>(requestHeaders); // requestHeaders is of HttpHeaders type
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(rawValidUrl) // rawValidURl = http://example.com/hotels
            .queryParams(
                    (LinkedMultiValueMap<String, String>) allRequestParams); // The allRequestParams must have been built for all the query params
    UriComponents uriComponents = builder.build().encode(); // encode() is to ensure that characters like {, }, are preserved and not encoded. Skip if not needed.
    ResponseEntity<Object> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,
            entity, String.class);
    return responseEntity;
}

public ResponseEntity<String> requestRestServerWithPostMethod()
{
    HttpEntity<?> entity = new HttpEntity<>(requestBody, requestHeaders); // requestBody is of string type and requestHeaders is of type HttpHeaders
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(rawValidUrl) // rawValidURl = http://example.com/hotels
            .queryParams(
                    (LinkedMultiValueMap<String, String>) allRequestParams); // The allRequestParams must have been built for all the query params
    UriComponents uriComponents = builder.build().encode(); // encode() is to ensure that characters like {, }, are preserved and not encoded. Skip if not needed.
    ResponseEntity<Object> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST,
            entity, String.class);
    return responseEntity;
}
Decastyle answered 20/11, 2018 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.