RestTemplate uriVariables未展开

我尝试使用弹簧RestTemplate.getForObject()访问rest端点,但我的uri变量未展开,并作为参数附加到url。 这是我到目前为止所得到的:

Map uriParams = new HashMap(); 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); 

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...被调用。 任何提示都表示赞赏。

我正在使用Spring 3.1.4.RELEASE

问候。

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

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

变为:

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

如果foo是2。

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

 String url = "http://www.sample.com?foo={fooValue}"; Map uriVariables = new HashMap(); uriVariables.put("fooValue", 2); // "http://www.sample.com?foo=2" restTemplate.getForObject(url, Object.class, uriVariables); 

RC。的接受答案是正确的,在url字符串中需要变量标记的params映射要替换为(“//www.sample.com?foo={foo}”中的“foo”被替换为映射的键“ foo“在你的params地图中)。

从技术上讲,也可以将params显式编码到URL String本身中,例如:

 endpointUrl = endpointUrl + "?method=login&input_type=" + DATA_TYPE + "&rest_data=" + rest_data.toString();