Java RestTemplate uriVariables 未扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20705377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:42:12  来源:igfitidea点击:

RestTemplate uriVariables not expanded

javaspringresttemplate

提问by user1145874

I try to access a rest endpoint by using springs RestTemplate.getForObject() but my uri variables are not expanded, and attached as parameters to the url. This is what I got so far:

我尝试使用 springs RestTemplate.getForObject() 访问休息端点,但我的 uri 变量没有扩展,并作为参数附加到 url。这是我到目前为止得到的:

Map<String, String> uriParams = new HashMap<String, String>();
uriParams.put("method", "login");
uriParams.put("input_type", DATA_TYPE);
uriParams.put("response_type", DATA_TYPE);
uriParams.put("rest_data", rest_data.toString());
String responseString = template.getForObject(endpointUrl, String.class, uriParams);

the value of the endpointUrl Variable is http://127.0.0.1/service/v4_1/rest.phpand it's exactely what it's called but I'd expect http://127.0.0.1/service/v4_1/rest.php?method=login&input_type...to be called. Any hints are appreciated.

endpointUrl 变量http://127.0.0.1/service/v4_1/rest.php的值正是它的名字,但我希望http://127.0.0.1/service/v4_1/rest.php?method=login&input_type...被调用。任何提示表示赞赏。

I'm using Spring 3.1.4.RELEASE

我正在使用 Spring 3.1.4.RELEASE

Regards.

问候。

采纳答案by user1145874

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

没有附加一些查询字符串逻辑,RestTemplate它基本上用{foo}它们的值替换变量:

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

becomes:

变成:

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

if foois 2.

如果foo是 2。

回答by Bane

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.

来自 user180100 的当前标记答案在技术上是正确的,但不是很明确。这是一个更明确的答案,以帮助那些在我身后的人,因为当我第一次阅读 zhe 的答案时,这对我来说没有意义。

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);