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?
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.
For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.
Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With