Jerseyを使用したJAX-RSクライアント
1. 概要
Jerseyは、RESTFul Webサービスを開発するためのオープンソースフレームワークです。 また、優れた組み込みクライアント機能も備えています。
このクイックチュートリアルでは、Jersey 2を使用したJAX-RSクライアントの作成について説明します。
ジャージーを使用したRESTfulWebサービスの作成に関する議論については、this articleを参照してください。
参考文献:
2. Mavenの依存関係
pom.xmlに必要な依存関係(Jersey JAX-RSクライアントの場合)を追加することから始めましょう。
org.glassfish.jersey.core
jersey-client
2.25.1
Jackson 2.xをJSONプロバイダーとして使用するには:
org.glassfish.jersey.media
jersey-media-json-jackson
2.25.1
これらの依存関係の最新バージョンは、jersey-clientおよびjersey-media-json-jacksonにあります。
3. ジャージーのRESTFulクライアント
hereで開発したJSONおよびXML REST APIを使用するJAX-RSクライアントを開発します(サービスがデプロイされ、URLにアクセスできることを確認する必要があります)。
3.1. リソース表現クラス
リソース表現クラスを見てみましょう。
@XmlRootElement
public class Employee {
private int id;
private String firstName;
// standard getters and setters
}
@XmlRootElementのようなJAXBアノテーションは、XMLサポートが必要な場合にのみ必要です。
3.2. Clientのインスタンスの作成
最初に必要なのは、Clientのインスタンスです。
Client client = ClientBuilder.newClient();
3.3. WebTargetの作成
Clientインスタンスを取得したら、ターゲットWebリソースのURIを使用してWebTargetを作成できます。
WebTarget webTarget
= client.target("http://localhost:8082/spring-jersey");
WebTargetを使用して、特定のリソースへのパスを定義できます。
WebTarget employeeWebTarget
= webTarget.path("resources/employees");
3.4. HTTPリクエスト呼び出しの構築
呼び出しビルダーインスタンスは、WebTarget.request()メソッドの1つで作成されます。
Invocation.Builder invocationBuilder
= employeeWebTarget.request(MediaType.APPLICATION_JSON);
XML形式の場合、MediaType.APPLICATION_XMLを使用できます。
3.5. HTTPリクエストの呼び出し
HTTP GETの呼び出し:
Response response
= invocationBuilder.get(Employee.class);
HTTP POSTの呼び出し:
Response response
= invocationBuilder
.post(Entity.entity(employee, MediaType.APPLICATION_JSON);
3.6. サンプルRESTクライアント
簡単なRESTクライアントの作成を始めましょう。 getJsonEmployee()メソッドは、従業員idに基づいてEmployeeオブジェクトを取得します。 REST Web Serviceによって返されるJSONは、返される前にEmployeeオブジェクトに逆シリアル化されます。
JAX-RS APIを流Usingに使用してWebターゲット、呼び出しビルダー、およびGET HTTPリクエストを呼び出します。
public class RestClient {
private static final String REST_URI
= "http://localhost:8082/spring-jersey/resources/employees";
private Client client = ClientBuilder.newClient();
public Employee getJsonEmployee(int id) {
return client
.target(REST_URI)
.path(String.valueOf(id))
.request(MediaType.APPLICATION_JSON)
.get(Employee.class);
}
//...
}
次に、POSTHTTPリクエストのメソッドを追加しましょう。 createJsonEmployee()メソッドは、Employeeの作成のためにREST Web Serviceを呼び出すことにより、Employeeを作成します。 クライアントAPIは、HTTP POSTメソッドを呼び出す前に、EmployeeオブジェクトをJSONに内部的にシリアル化します。
public Response createJsonEmployee(Employee emp) {
return client
.target(REST_URI)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(emp, MediaType.APPLICATION_JSON));
}
4. クライアントのテスト
JUnitを使用してクライアントをテストしてみましょう。
public class JerseyClientLiveTest {
public static final int HTTP_CREATED = 201;
private RestClient client = new RestClient();
@Test
public void givenCorrectObject_whenCorrectJsonRequest_thenResponseCodeCreated() {
Employee emp = new Employee(6, "Johny");
Response response = client.createJsonEmployee(emp);
assertEquals(response.getStatus(), HTTP_CREATED);
}
}
5. 結論
この記事では、Jersey 2を使用したJAX-RSクライアントを紹介し、簡単なRESTFul Javaクライアントを開発しました。
いつものように、完全なソースコードはthis Github projectで入手できます。