0

I am using spring-web version 5.0.6.RELEASE.

I tried calling the GET exchange method with the URI variable map, but these URI variables are not set to the final URL which hit the external system.

My code snippet is as below

Map<String,Object> queryParam =  new HashMap<>();
queryParam.put("id", 1);
queryParam.put("name", "ABC");
ResponseEntity<List<MyResponse>> responseEntity = restTemplate.exchange(serverUrl.toString(),HttpMethod.GET,null,
                        new ParameterizedTypeReference<List<MyResponse>>(){}, queryParam);

When I debugged , I found RestTemplate.execute method have a URI creation code URI expanded = getUriTemplateHandler().expand(url, uriVariables); which does not set the URI params to URI class.

I furthur debugged and found that in DefaultUriBuilderFactory.build()method the URI variable is not passed or set to URI component builder

UriComponents uriComponents = this.uriComponentsBuilder.build().expand(uriVars); which causes HierarchicalUriComponents expandInternal(UriTemplateVariables uriVariables) method of HierarchicalUriComponents class to set the URI variable as empty.

Can anyone help me understand if I am doing anything wrong here or Spring web have any bug for such cases?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MishraJi
  • 304
  • 2
  • 6
  • 18
  • You haven't shared the most important bits. What does `serverUrl.toString()` looks like? The expand of URI variables require variables to be set in the url in the first place. Perhaps https://stackoverflow.com/questions/20705377/resttemplate-urivariables-not-expanded answers your question? – Stephane Nicoll Dec 26 '19 at 08:51
  • @StephaneNicoll I got the answer , so here to use the queryParam MAP , we have to set the variable name in URL string like `http://localhost:8080/service?id={id}&name={name}` , however I think this should be designed in a different way or may be we can have better documentation with example as we can see lot many developer are confused with its usage. Thanks – MishraJi Dec 28 '19 at 15:59

0 Answers0