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
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:
I would use
buildAndExpand
fromUriComponentsBuilder
to pass all types of URI parameters.For example:
One-liner using TestRestTemplate.exchange function with parameters map.
The params map initialized like this is a groovy initializer*