Issue
I want to make REST call using spring RestTemplate, the URL contains some optional query params. The URL looks something like
url = example.com/param1={param1}¶m2={param2}
I pass params as map to restTemplate using exchange method
restTemplate.exchange(url, method, payLoad, String.class, params)
The final URL is example.com/param1=somevalue¶m2= since param2 was not present in params map.
I want to remove param2 from the request, that is, the final URL should contain only param1 and URL should look like example.com/param1=somevalue
Solution
You can use UriComponentsBuilder and provide desired params (not nulls).
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("example.com");
builder.replaceQueryParam("param1", param1value);
...
restTemplate.exchange(builder.build().encode().toUri(),
httpMethod,
requestEntity,
String.class)
Answered By - StanislavL
Answer Checked By - Timothy Miller (JavaFixing Admin)