Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate: How to send URL and query parameters together

I am trying to pass path param and query params in a URL but I am getting a weird error. below is the code

    String url = "http://test.com/Services/rest/{id}/Identifier"     Map<String, String> params = new HashMap<String, String>();     params.put("id", "1234");     UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)                                         .queryParam("name", "myName");     String uriBuilder = builder.build().encode().toUriString();     restTemplate.exchange(uriBuilder , HttpMethod.PUT, requestEntity,                     class_p, params); 

and my url is becoming http://test.com/Services/rest/%7Bid%7D/Identifier?name=myName

what should I do to make it work. I am expecting http://test.com/Services/rest/{id}/Identifier?name=myName so that params will add id to the url

please suggest. thanks in Advance

like image 967
Shiva Avatar asked Mar 14 '16 21:03

Shiva


People also ask

How do you pass URL and query parameters in Spring REST client?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol. The same parameters passed as URL parameters in the previous example are passed as Query parameters here.

Can I pass URL as query parameter?

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.


2 Answers

I would use buildAndExpand from UriComponentsBuilder to pass all types of URI parameters.

For example:

String url = "http://test.com/solarSystem/planets/{planet}/moons/{moon}";  // URI (URL) parameters Map<String, String> urlParams = new HashMap<>(); urlParams.put("planet", "Mars"); urlParams.put("moon", "Phobos");  // Query parameters UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)         // Add query parameter         .queryParam("firstName", "Mark")         .queryParam("lastName", "Watney");  System.out.println(builder.buildAndExpand(urlParams).toUri()); /**  * Console output:  * http://test.com/solarSystem/planets/Mars/moons/Phobos?firstName=Mark&lastName=Watney  */  restTemplate.exchange(builder.buildAndExpand(urlParams).toUri() , HttpMethod.PUT,         requestEntity, class_p);  /**  * Log entry:  * org.springframework.web.client.RestTemplate Created PUT request for "http://test.com/solarSystem/planets/Mars/moons/Phobos?firstName=Mark&lastName=Watney"  */ 
like image 193
Michal Foksa Avatar answered Sep 21 '22 15:09

Michal Foksa


An issue with the answer from Michal Foksa is that it adds the query parameters first, and then expands the path variables. If query parameter contains parenthesis, e.g. {foobar}, this will cause an exception.

The safe way is to expand the path variables first, and then add the query parameters:

String url = "http://test.com/Services/rest/{id}/Identifier"; Map<String, String> params = new HashMap<String, String>(); params.put("id", "1234"); URI uri = UriComponentsBuilder.fromUriString(url)         .buildAndExpand(params)         .toUri(); uri = UriComponentsBuilder         .fromUri(uri)         .queryParam("name", "myName")         .build()         .toUri(); restTemplate.exchange(uri , HttpMethod.PUT, requestEntity, class_p); 
like image 36
holmis83 Avatar answered Sep 21 '22 15:09

holmis83