3

json 応答を返す resful api を呼び出す必要があります。jersey クライアント API の使用を考えていますが、HttpClient を直接使用してから GSON を使用して応答を Java オブジェクトに変換するよりも優れているかどうかはわかりません。

4

2 に答える 2

11

コーディング効率の観点からは、 Jersey クライアントはHttpClient よりもはるかに優れています。検討:

// Jersey client
WebResource resource = Client.create().resource("http://foo.com")
resource.path("widgets").entity(someWidget).type(APPLICATION_JSON).post();
Wodget wodget = resource.path("widgets/wodget").accept(APPLICATION_XML).get(Wodget.class);

とは対照的に:

// HttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://foo.com/widgets");
HttpEntity someWidgetEntity = ... // something with GSON to marshal the 'someWidget'
httpPost.setEntity(someWidgetEntity);
HttpResponse response = httpclient.execute(httpPost);

HttpGet httpGet = new HttpGet("http://foo.com/widgets/wodget");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
    ... // something with GSON to read in the wodget
}

さらに、Jersey クライアントは HTTP インタラクションのために内部で HttpClient を使用できるという事実を追加すると、簡素化されたインターフェイスと、広く使用されている汎用性の高い HTTP クライアント ライブラリの機能という、両方の世界を最大限に活用できます。

注: 上記のコードは完全にテストされていませんが、形状はほぼ正確です。

于 2013-03-05T01:28:46.390 に答える