web-dev-qa-db-ja.com

RestTemplate uriVariablesが展開されない

Springs RestTemplate.getForObject()を使用してRESTエンドポイントにアクセスしようとしていますが、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);

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を使用しています。リリース

よろしく。

26
user1145874

RestTemplate にはクエリ文字列ロジックが追加されていません。基本的に、{foo}のような変数をその値で置き換えます。

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

になる:

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

fooが2の場合。

33
user180100

現在マークされている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);
10
Bane

RC。の承認済み回答は、URL文字列内の変数マーカーを必要とするparamsマップが正しい( "//www.sample.com?foo={foo}"の "foo"は、 "によってマップされたキーで置き換えられるparamsマップのfoo ")。

技術的には、次のようにパラメータをURL文字列自体に明示的にコーディングすることもできます。

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