RestTemplate uriVariables not expanded

白昼怎懂夜的黑 提交于 2019-12-03 22:25:24

There is no append some query string logic in RestTemplate it basically replace variable like {foo} by their value:

http://www.sample.com?foo={foo}

becomes:

http://www.sample.com?foo=2

if foo is 2.

The currently-marked answer from user180100 is technically correct but not very explicit. Here is a more explicit answer, to help those coming along behind me, because when I first read zhe's answer it didn't make sense to me.

String url = "http://www.sample.com?foo={fooValue}";

Map<String, String> uriVariables = new HashMap();
uriVariables.put("fooValue", 2);

// "http://www.sample.com?foo=2"
restTemplate.getForObject(url, Object.class, uriVariables);

RC.'s Accepted Answer is correct about the params map needing variable markers in the url String to replace into ("foo" in "//www.sample.com?foo={foo}" gets replaced with the key mapped by "foo" in your params map).

It is technically also possible to explicitly code the params into the URL String itself to begin with like:

endpointUrl = endpointUrl + "?method=login&input_type=" + DATA_TYPE + "&rest_data=" + rest_data.toString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!