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?
In Spring Web 4.3.6 I also see
That means you don't have to create an ugly map
So if you have this url
You can either do
or
Since at least Spring 3, instead of using
UriComponentsBuilder
to build the URL (which is a bit verbose), many of theRestTemplate
methods accept placeholders in the path for parameters (not justexchange
).From the documentation:
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.