Spring RestTemplate GET with parameters
Asked Answered
S

18

397

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?

Shoifet answered 28/11, 2011 at 14:21 Comment(0)
S
32

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.

Shoifet answered 29/11, 2011 at 15:7 Comment(4)
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.Tedmund
I seriously do not understand why this answer has green tick.Mccracken
because he is the OPNikolia
So what is your solution? Thanks!Eridanus
J
686

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
);
Jovi answered 21/8, 2014 at 19:29 Comment(13)
Great tip. Just changed exchange to getForEntity: restTemplate.getForEntity(builder.build().encode().toUri(), String.class); for simplicity.Lance
@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().Grooved
@Christophe L Can u show how could i receive these string parameters in the server side??Autography
There is a shortcut to get the URI: just call builder.toUriString()Seigel
Spring docs for UriComponentsBuilder. Guide explaining various use cases of UriComponentsBuilderDoviedow
How to add Path parameters? Please help.Cartomancy
This one working on my windows local machine. But throwing 403 error on my Linux server deployed with tomcat. How can i fix this?Thumbsdown
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/…Stella
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.Fossiliferous
You might change the type of response to ResponseEntity<String>, so you can see also, that i.e the status code is available.Ober
As @Fossiliferous 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/…Cornemuse
In some cases it is not recommended to use a URL with parameters directly in the string : cloud.spring.io/spring-cloud-netflix/multi/…Galliwasp
Updated to use template parameters as recommended, which will avoid metrics explosion.Jovi
P
265

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.

Pony answered 21/5, 2013 at 18:18 Comment(4)
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.Cataclysm
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 MicrometerPrase
@Prase — 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).Energy
exactly! and you can check the URL result by calling: restTemplate.getUriTemplateHandler().expand(“/some/{some}/{other}”, some, other); see org.springframework.web.util.UriTemplateHandlerThoracotomy
S
71

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.

Samarasamarang answered 29/4, 2018 at 17:48 Comment(0)
A
34
    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)

Accessible answered 30/1, 2019 at 18:17 Comment(0)
S
32

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.

Shoifet answered 29/11, 2011 at 15:7 Comment(4)
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.Tedmund
I seriously do not understand why this answer has green tick.Mccracken
because he is the OPNikolia
So what is your solution? Thanks!Eridanus
O
21

I was attempting something similar, and the RoboSpice example helped me work it out:

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");

HttpEntity<String> request = new HttpEntity<>(input, createHeader());

String url = "http://awesomesite.org";
Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
uriBuilder.appendQueryParameter(key, value);
uriBuilder.appendQueryParameter(key, value);
...

String url = uriBuilder.build().toString();

HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request , String.class);
Ollie answered 27/7, 2013 at 21:33 Comment(0)
I
17

Converting of a hash map to a string of query parameters:

Map<String, 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);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, String> entry : params.entrySet()) {
    builder.queryParam(entry.getKey(), entry.getValue());
}

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");

HttpEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, new HttpEntity(headers), String.class);
Inspection answered 8/8, 2019 at 14:14 Comment(0)
N
11

In Spring Web 4.3.6 I also see

public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)

That means you don't have to create an ugly map

So if you have this url

http://my-url/action?param1={param1}&param2={param2}

You can either do

restTemplate.getForObject(url, Response.class, param1, param2)

or

restTemplate.getForObject(url, Response.class, param [])
Nikolia answered 29/11, 2018 at 17:47 Comment(0)
A
5

I take different approach, you may agree or not but I want to control from .properties file instead of compiled Java code

Inside application.properties file

endpoint.url = https://yourHost/resource?requestParam1={0}&requestParam2={1}

Java code goes here, you can write if or switch condition to find out if endpoint URL in .properties file has @PathVariable (contains {}) or @RequestParam (yourURL?key=value) etc... then invoke method accordingly... that way its dynamic and not need to code change in future one stop shop...

I'm trying to give more of idea than actual code here ...try to write generic method each for @RequestParam, and @PathVariable etc... then call accordingly when needed

  @Value("${endpoint.url}")
  private String endpointURL;
  // you can use variable args feature in Java
  public String requestParamMethodNameHere(String value1, String value2) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate
           .getMessageConverters()
           .add(new MappingJackson2HttpMessageConverter());

    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
    HttpEntity<String> entity = new HttpEntity<>(headers);

    try {
      String formatted_URL = MessageFormat.format(endpointURL, value1, value2);
      ResponseEntity<String> response = restTemplate.exchange(
                    formatted_URL ,
                    HttpMethod.GET,
                    entity,
                    String.class);
     return response.getBody();
    } catch (Exception e) { e.printStackTrace(); }
Aranyaka answered 25/9, 2016 at 2:3 Comment(0)
P
4

If you pass non-parametrized params for RestTemplate, you'll have one Metrics for everyone single different URL that you pass, considering the parameters. You would like to use parametrized urls:

http://my-url/action?param1={param1}&param2={param2}

instead of

http://my-url/action?param1=XXXX&param2=YYYY

The second case is what you get by using UriComponentsBuilder class.

One way to implement the first behavior is the following:

Map<String, Object> params = new HashMap<>();
params.put("param1", "XXXX");
params.put("param2", "YYYY");

String url = "http://my-url/action?%s";

String parametrizedArgs = params.keySet().stream().map(k ->
    String.format("%s={%s}", k, k)
).collect(Collectors.joining("&"));

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);

restTemplate.exchange(String.format(url, parametrizedArgs), HttpMethod.GET, entity, String.class, params);
Proceeds answered 4/7, 2018 at 14:27 Comment(0)
D
3
public static void main(String[] args) {
         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
         final String url = "https://host:port/contract/{code}";
         Map<String, String> params = new HashMap<String, String>();
         params.put("code", "123456");
         HttpEntity<?> httpEntity  = new HttpEntity<>(httpHeaders); 
         RestTemplate restTemplate  = new RestTemplate();
         restTemplate.exchange(url, HttpMethod.GET, httpEntity,String.class, params);
    }
Dracaena answered 17/5, 2018 at 5:26 Comment(0)
H
3

I am providing a code snippet of RestTemplate GET method with path param example

public ResponseEntity<String> getName(int id) {
    final String url = "http://localhost:8080/springrestexample/employee/name?id={id}";
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", id);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity request = new HttpEntity(headers);
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, String.class, params);
    return response;
}
Hippy answered 26/2, 2021 at 15:4 Comment(0)
N
3

One more solution as method:

private String execute(String url, Map<String, String> params) {
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(url)
    // predefined params
            .queryParam("user", "userValue")
            .queryParam("password", "passwordValue");
    params.forEach(uriBuilder::queryParam);
    HttpHeaders headers = new HttpHeaders() {{
        setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        setAccept(List.of(MediaType.APPLICATION_JSON));
    }};
    ResponseEntity<String> request = restTemplate.exchange(uriBuilder.toUriString(), 
                HttpMethod.GET, new HttpEntity<>(headers), String.class);
    return request.getBody();

}
Novelette answered 5/12, 2022 at 15:16 Comment(0)
F
2

You can use follow code for String.

URL_EXAMPLE="http://{domain}/Index.php?Username={user}&password={password}";

String domain = "example.com";
String user = "user";
String password = "password";

String data=this.restTemplate.getForObject(URL_EXAMPLE,String.class,domain,user,password);
Foolish answered 9/3, 2022 at 6:22 Comment(0)
A
1

If your url is http://localhost:8080/context path?msisdn={msisdn}&email={email}

then

Map<String,Object> queryParams=new HashMap<>();
queryParams.put("msisdn",your value)
queryParams.put("email",your value)

works for resttemplate exchange method as described by you

Autonomy answered 21/11, 2019 at 8:14 Comment(0)
R
0

hi i build url with query params using this code:

UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("bikerPhoneNumber", "phoneNumberString")
                .toUriString();
Relapse answered 17/7, 2022 at 12:1 Comment(0)
S
0

See I observe two things, the query parameter (@RequestParam) or the dynamic URLs (@PathVariable) can be passed in two ways i.e.

  1. Passing the data as Array.
  2. Using the map.

In both the pattern, one thing is common. the data needs to passed dynamically and we can mark those places using curly braces.{} ex.

https://api.themoviedb.org/3/movie/now_playing?api_key={api_key}

Spring Documentation Now we need to pass the data,

Style 1

Map<String,String> data=new LinkedHashMap<String,String>();
data.put("api_key", api_key);
template.getForObject(url, TMDB.class,data);

Style 2

Object[] arr= {api_key};
template.getForObject(url, TMDB.class,arr);
Sabbat answered 28/2, 2023 at 17:46 Comment(0)
R
0

You can use the method 'UriComponentsBuilder.queryParams', but first convert the map into 'MultiValueMap'.

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);

    Map<String, Object> 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);

    LinkedMultiValueMap<String, String> queryParams = params.entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, e -> List.of(String.valueOf(e.getValue())),
                    (a, b) -> a, LinkedMultiValueMap::new));

    URI uri = UriComponentsBuilder.fromHttpUrl(urlTemplate)
            .queryParams(queryParams).encode().build().toUri();

    HttpEntity<String> response = restTemplate.exchange(
            new RequestEntity<>(headers, HttpMethod.GET, uri), String.class);
Romanov answered 13/1 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.