Spring RestTemplate GET with parameters

590,208

Solution 1

OK, so I'm being an idiot and I'm confusing query parameters with url parameters. I was kinda hoping there would be a nicer way to populate my query parameters rather than an ugly concatenated String but there we are. It's simply a case of build the URL with the correct parameters. If you pass it as a String Spring will also take care of the encoding for you.

Solution 2

To easily manipulate URLs / path / params / etc., you can use Spring's UriComponentsBuilder class to create a URL template with placehoders for the parameters, then provide the value for those parameters in the RestOperations.exchange(...) call. It's cleaner than manually concatenating strings and it takes care of the URL encoding for you:

HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> entity = new HttpEntity<>(headers);

String urlTemplate = UriComponentsBuilder.fromHttpUrl(url)
        .queryParam("msisdn", "{msisdn}")
        .queryParam("email", "{email}")
        .queryParam("clientVersion", "{clientVersion}")
        .queryParam("clientType", "{clientType}")
        .queryParam("issuerName", "{issuerName}")
        .queryParam("applicationName", "{applicationName}")
        .encode()
        .toUriString();

Map<String, ?> params = new HashMap<>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);

HttpEntity<String> response = restOperations.exchange(
        urlTemplate,
        HttpMethod.GET,
        entity,
        String.class,
        params
);

Solution 3

The uriVariables are also expanded in the query string. For example, the following call will expand values for both, account and name:

restTemplate.exchange("http://my-rest-url.org/rest/account/{account}?name={name}",
    HttpMethod.GET,
    httpEntity,
    clazz,
    "my-account",
    "my-name"
);

so the actual request url will be

http://my-rest-url.org/rest/account/my-account?name=my-name

Look at HierarchicalUriComponents.expandInternal(UriTemplateVariables) for more details. Version of Spring is 3.1.3.

Solution 4

Since at least Spring 3, instead of using UriComponentsBuilder to build the URL (which is a bit verbose), many of the RestTemplate methods accept placeholders in the path for parameters (not just exchange).

From the documentation:

Many of the RestTemplate methods accepts a URI template and URI template variables, either as a String vararg, or as Map<String,String>.

For example with a String vararg:

restTemplate.getForObject(
   "http://example.com/hotels/{hotel}/rooms/{room}", String.class, "42", "21");

Or with a Map<String, String>:

Map<String, String> vars = new HashMap<>();
vars.put("hotel", "42");
vars.put("room", "21");

restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{room}", 
    String.class, vars);

Reference: https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#rest-resttemplate-uri

If you look at the JavaDoc for RestTemplate and search for "URI Template", you can see which methods you can use placeholders with.

Solution 5

    String uri = http://my-rest-url.org/rest/account/{account};

    Map<String, String> uriParam = new HashMap<>();
    uriParam.put("account", "my_account");

    UriComponents builder = UriComponentsBuilder.fromHttpUrl(uri)
                .queryParam("pageSize","2")
                        .queryParam("page","0")
                        .queryParam("name","my_name").build();

    HttpEntity<String> requestEntity = new HttpEntity<>(null, getHeaders());

    ResponseEntity<String> strResponse = restTemplate.exchange(builder.toUriString(),HttpMethod.GET, requestEntity,
                        String.class,uriParam);

    //final URL: http://my-rest-url.org/rest/account/my_account?pageSize=2&page=0&name=my_name

RestTemplate: Build dynamic URI using UriComponents (URI variable and Request parameters)

Share:
590,208
Elwood
Author by

Elwood

Updated on July 08, 2022

Comments

  • Elwood
    Elwood almost 2 years

    I have to make a REST call that includes custom headers and query parameters. I set my HttpEntity with just the headers (no body), and I use the RestTemplate.exchange() method as follows:

    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", "application/json");
    
    Map<String, String> params = new HashMap<String, String>();
    params.put("msisdn", msisdn);
    params.put("email", email);
    params.put("clientVersion", clientVersion);
    params.put("clientType", clientType);
    params.put("issuerName", issuerName);
    params.put("applicationName", applicationName);
    
    HttpEntity entity = new HttpEntity(headers);
    
    HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);
    

    This fails at the client end with the dispatcher servlet being unable to resolve the request to a handler. Having debugged it, it looks like the request parameters are not being sent.

    When I do a an exchange with a POST using a request body and no query parameters it works just fine.

    Does anyone have any ideas?

  • yathirigan
    yathirigan almost 9 years
    did it work for you ? i followed the same approach of using the UriComponentsBuilder but, at the target URL, when i do a request.getAttribute(), i get null.
  • Fernando M. Pinheiro
    Fernando M. Pinheiro over 8 years
    Great tip. Just changed exchange to getForEntity: restTemplate.getForEntity(builder.build().encode().toUri(), String.class); for simplicity.
  • mirzmaster
    mirzmaster over 8 years
    @FernandoM.Pinheiro: You're correct, but if you're expecting a generic type in the response, then you need to use exchange and provide a ParameterizedTypeReference. The example can be further simplified though, replacing builder.build().encode().toUri() with builder.toUriString().
  • KJEjava48
    KJEjava48 over 7 years
    @Christophe L Can u show how could i receive these string parameters in the server side??
  • Michael Piefel
    Michael Piefel over 7 years
    There is a shortcut to get the URI: just call builder.toUriString()
  • Chacko Mathew
    Chacko Mathew over 6 years
    Spring docs for UriComponentsBuilder. Guide explaining various use cases of UriComponentsBuilder
  • Angshuman Agarwal
    Angshuman Agarwal over 6 years
    Thanks - Very simple solution
  • Pradeep
    Pradeep over 6 years
    I seriously do not understand why this answer has green tick.
  • Stephen Rudolph
    Stephen Rudolph about 6 years
    And when creating the RestTemplate instance, you can specify how those query parameter values will be expanded by specifying the DefaultUriTemplateHandler (prior to Spring 5) or DefaultUriBuilderFactory (Spring 5+). This is useful when you wish to encode additional characters such as !, (, ), etc.
  • Doug
    Doug about 6 years
    My URL has 10+ parameters, any way to achieve the same with an object/map instead of listing every variable? I cannot use UriComponentsBuilder either as it is causing it to generate a different metric for each request with Micrometer
  • Abdul
    Abdul almost 6 years
    How to add Path parameters? Please help.
  • Kalpesh Soni
    Kalpesh Soni over 5 years
    because he is the OP
  • Mohammed Javad
    Mohammed Javad almost 5 years
    This one working on my windows local machine. But throwing 403 error on my Linux server deployed with tomcat. How can i fix this?
  • M. Justin
    M. Justin almost 5 years
    @Doug — RestTemplate has parallel methods for specifying either a positional array of values (Object... uriVariables) or a map of named values (Map<String, ?> uriVariables). Sounds like the map version is what you want: restTemplate.exchange(url, HttpMethod.GET, httpEntity, clazz, urlVariablesMap).
  • ChrisK
    ChrisK over 4 years
    If anyone happens to use Prometheus/Micrometer then this may cause an explosion of logs. http_client* metrics will be recorded for every possible URI formed by UriComponentsBuilder, e.g. /person/1 and /person/2. This is because Spring RestTemplate Metrics and WebClient Metrics want to populate the URI tag prior to variable substitution but cannot since restTemplate.exchange() already receives a populated URI as first argument. The solution is pavel's answer. See: docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/html‌​/…
  • Raymond Chen
    Raymond Chen over 4 years
    So what is your solution? Thanks!
  • steve
    steve over 4 years
    Careful! You will get double encoding if you do it that way. builder.queryParam("value", "some&value") will be encoded during builder.toUriString(), "some%26value", then again in the 'exchange()', "some%2526value". Better to pass in uriVariables instead.
  • Spille
    Spille almost 4 years
    You might change the type of response to ResponseEntity<String>, so you can see also, that i.e the status code is available.
  • Denis Orlov
    Denis Orlov over 3 years
    exactly! and you can check the URL result by calling: restTemplate.getUriTemplateHandler().expand(“/some/{some}/{o‌​ther}”, some, other); see org.springframework.web.util.UriTemplateHandler
  • riverhorse
    riverhorse over 3 years
    As @steve said above you risk getting double encoded values if you do as in this answer, including if you have spaces in your param value. This can be fixed if you use .build().toUriString() instead of just plain .toUriString(). This skips calling .encode(), which does the trick. See docs.spring.io/spring-framework/docs/current/javadoc-api/org‌​/…
  • Nemolovich
    Nemolovich over 2 years
    In some cases it is not recommended to use a URL with parameters directly in the string : cloud.spring.io/spring-cloud-netflix/multi/…
  • Christophe L
    Christophe L over 2 years
    Updated to use template parameters as recommended, which will avoid metrics explosion.