6

I'm using org.springframework.web.client.resttemplate and I need to pass query params to my GET request.

Does anyone have any example of this?

Jim
  • 3,173
  • 1
  • 16
  • 26
Nir
  • 1,204
  • 5
  • 19
  • 35
  • Your question doesn't make much sense. If you are putting variables in the request you are sending then it would be POST, I've answered that below. – Jim Jun 17 '13 at 09:33
  • 3
    @Jim it is perefectly sensible to send url parameters in a get request, why wouldn;t it be - thats what they are for – NimChimpsky Jun 17 '13 at 09:42
  • 1
    @NimChimpsky to clarify, query params I think is ambiguous and perhaps so is my comment. I agree that having parameters in the URL is acceptable, but not in the body of the HTML request. – Jim Jun 17 '13 at 10:07
  • 1
    ... and here's why http://stackoverflow.com/a/983458/106261 – NimChimpsky Jun 17 '13 at 10:31

2 Answers2

7

Just pass them as part of the url string. Spring will do the rest, shown below are two types of parameter - an uri parameter and a request parameter:

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

Docs here.

dube
  • 4,377
  • 2
  • 20
  • 36
NimChimpsky
  • 43,542
  • 55
  • 186
  • 295
  • Thanks for the help. there is an issue though when one of your uri paramter is pointing a json, for example: ?{searchKey}={searchValue}&page={page}&start={start}&limit={limit}&sort=[{property:\"{propertyValue}\",\"direction\":\"{direction}\"}]" – Nir Jun 20 '13 at 08:41
  • @Nir pass the json as one string and use jackson/gson to convert into a pojo server side – NimChimpsky Jun 20 '13 at 08:43
  • Thanks @NumChimpsky -> Do you have an example of this implementation? – Nir Jun 24 '13 at 11:57
2

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() (useful when you want to send JSON or anything that has symbols { and } as part of the params), 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;
}
piet.t
  • 11,035
  • 20
  • 40
  • 49
noob
  • 698
  • 1
  • 8
  • 22