1

FusionTableAPIをREST呼び出ししようとしています。私はRESTに春のテンプレートを使用しています

URLを次のように定義している場合:

String URI = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableid/import";

例外 :

 Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized

これは問題なく、そのヒットグーグルサーバーを意味します。

しかし、私が使用するとき:

URL  :String URI = "https://www.googleapis.com/upload/fusiontables/v1/tables/tableid/import"+"&Authorization:""&Content-Type: application/octet-stream";

その投げる実行:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 404 Not Found

私には理解できません。

4

1 に答える 1

4

URL文字列にリクエストヘッダーを追加していますが、これは正しくありません。これらの2つのヘッダー:

  • 承認: ""
  • コンテンツタイプ:アプリケーション/オクテットストリーム

リクエストURLに追加しないでください。代わりに、Apache HttpComponentsを使用している場合:

final HttpPost post = new HttpPost();
post.addHeader("Authorization", "");
post.addHeader("Content-Type", "application/octet-stream");

または、HttpUrlConnectionを使用します。

conn.setRequestProperty("Authorization", "");
conn.setRequestProperty("Content-Type", "application/octet-stream");

または、Spring RestTemplateを使用する場合:

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "");
headers.add("Content-Type", "application/octet-stream");
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, headers);

RestTemplateHttpEntityのドキュメントページには、Spring固有のオプションに関する大量の情報があります。

于 2012-12-16T07:53:08.467 に答える