2.19. MicroProfile REST クライアント
MicroProfile REST クライアントはテクノロジープレビューとしてのみ提供されます。テクノロジープレビューの機能は、Red Hat の本番環境のサービスレベルアグリーメント (SLA) ではサポートされず、機能的に完全ではないことがあるため、Red Hat は本番環境での使用は推奨しません。テクノロジープレビューの機能は、最新の製品機能をいち早く提供して、開発段階で機能のテストを行いフィードバックを提供していただくことを目的としています。
テクノロジープレビュー機能のサポート範囲は、Red Hat カスタマーポータルの テクノロジープレビュー機能のサポート範囲 を参照してください。
JBoss EAP 7.4 は、HTTP 上で RESTful サービスを呼び出すためにタイプセーフなアプローチを提供するために Jakarta RESTful Web Services 2.1 クライアント API 上に構築される MicroProfile REST クライアント 1.4.x をサポートします。MicroProfile Type Safe REST クライアントは、Java インターフェイスとして定義されます。MicroProfile REST クライアントでは、実行可能コードでクライアントアプリケーションの書き込みを行うことができます。
MicroProfile REST クライアントは以下を有効にします。
- 直感的構文
- プロバイダーのプログラムによる登録
- プロバイダーの宣言的登録
- ヘッダーの宣言的仕様
- サーバー上のヘッダーの伝搬
-
ResponseExceptionMapper - Jakarta Contexts and Dependency Injection の統合
2.19.1. 直感的構文 リンクのコピーリンクがクリップボードにコピーされました!
MicroProfile REST クライアントは、CORBA、Java Remote Method Invocation (RMI)、JBoss Remoting Project、および RESTEasy にも実装されている分散オブジェクト通信のバージョンを有効にします。たとえば、リソースについて考えてみましょう。
@Path("resource")
public class TestResource {
@Path("test")
@GET
String test() {
return "test";
}
}
TestResource クラスにアクセスする Jakarta RESTful Web Services のネイティブな方法は次のとおりです。
Client client = ClientBuilder.newClient();
String response = client.target("http://localhost:8081/test").request().get(String.class);
ただし、Microprofile REST クライアントは test() メソッドを直接呼び出すことで、より直感的な構文をサポートします。
@Path("resource")
public interface TestResourceIntf {
@Path("test")
@GET
public String test();
}
TestResourceIntf service = RestClientBuilder.newBuilder()
.baseUrl(http://localhost:8081/))
.build(TestResourceIntf.class);
String s = service.test();
上記の例では、TestResource クラスへの呼び出しは、呼び出し service.test() が示すように TestResourceIntf クラスにより大幅に容易になります。
以下の例は、TestResourceIntf クラスのより詳細なバージョンです。
@Path("resource")
public interface TestResourceIntf2 {
@Path("test/{path}")mes("text/plain")
@Produces("text/html")
@POST
public String test(@PathParam("path") String path, @QueryParam("query") String query, String entity);
}
service.test("p", "q", "e") メソッドを呼び出すと、以下のような HTTP メッセージが生成されます。
POST /resource/test/p/?query=q HTTP/1.1
Accept: text/html
Content-Type: text/plain
Content-Length: 1
e
2.19.2. プロバイダーのプログラムによる登録 リンクのコピーリンクがクリップボードにコピーされました!
MicroProfile REST クライアントでは、プロバイダーを登録してクライアント環境を設定することもできます。例を以下に示します。
TestResourceIntf service = RestClientBuilder.newBuilder()
.baseUrl(http://localhost:8081/))
.register(MyClientResponseFilter.class)
.register(MyMessageBodyReader.class)
.build(TestResourceIntf.class);
2.19.3. プロバイダーの宣言型登録 リンクのコピーリンクがクリップボードにコピーされました!
以下のように org.eclipse.microprofile.rest.client.annotation.RegisterProvider アノテーションをターゲットインターフェイスに追加して、プロバイダーを宣言的に登録することもできます。
@Path("resource")
@RegisterProvider(MyClientResponseFilter.class)
@RegisterProvider(MyMessageBodyReader.class)
public interface TestResourceIntf2 {
@Path("test/{path}")
@Consumes("text/plain")
@Produces("text/html")
@POST
public String test(@PathParam("path") String path, @QueryParam("query") String query, String entity);
}
MyClientResponseFilter クラスと MyMessageBodyReader クラスをアノテーションで宣言すると、RestClientBuilder.register() メソッドを呼び出す必要がなくなります。
2.19.4. ヘッダーの宣言的仕様 リンクのコピーリンクがクリップボードにコピーされました!
HTTP リクエストのヘッダーは、以下の方法で指定できます。
- リソースメソッドパラメーターのいずれかにアノテーションを付けます。
-
org.eclipse.microprofile.rest.client.annotation.ClientHeaderParamアノテーションを宣言で使用。
以下の例では、@HeaderValue アノテーションを持つリソースメソッドパラメーターのいずれかにアノテーションを付け、ヘッダーの設定を示しています。
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
String contentLang(@HeaderParam(HttpHeaders.CONTENT_LANGUAGE) String contentLanguage, String subject);
以下の例は、org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam アノテーションを使用してヘッダーを設定する例になります。
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@ClientHeaderParam(name=HttpHeaders.CONTENT_LANGUAGE, value="{getLanguage}")
String contentLang(String subject);
default String getLanguage() {
return ...;
}
2.19.5. サーバー上のヘッダーの伝搬 リンクのコピーリンクがクリップボードにコピーされました!
org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory のインスタンスが有効であれば、受信ヘッダーの送信要求に一括転送できます。デフォルトのインスタンス org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl は、コンマ区切りの設定プロパティー org.eclipse.microprofile.rest.client.propagateHeaders に一覧表示される着信ヘッダーで設定されるマップを返します。ClientHeadersFactory インターフェイスをインスタンス化するルールは次のとおりです。
-
Jakarta RESTful Web Services リクエストのコンテキストで呼び出される
ClientHeadersFactoryインスタンス は、@Contextアノテーションが付けられたフィールドおよびメソッドの注入をサポートする必要があります。 -
Jakarta Contexts and Dependency Injection によって管理される
ClientHeadersFactoryインスタンスは、適切な Jakarta Contexts and Dependency Injection-managed インスタンスを使用する必要があります。@Injectインジェクションもサポートする必要があります。
org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory インターフェイスは以下のように定義されます。
public interface ClientHeadersFactory {
/**
* Updates the HTTP headers to send to the remote service. Note that providers
* on the outbound processing chain could further update the headers.
*
* @param incomingHeaders - the map of headers from the inbound Jakarta RESTful Web Services request. This will
* be an empty map if the associated client interface is not part of a Jakarta RESTful Web Services request.
* @param clientOutgoingHeaders - the read-only map of header parameters specified on the
* client interface.
* @return a map of HTTP headers to merge with the clientOutgoingHeaders to be sent to
* the remote service.
*/
MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders,
MultivaluedMap<String, String> clientOutgoingHeaders);
}
ClientHeadersFactory インターフェイスの詳細は、ClientHeadersFactory Javadoc を参照してください。
2.19.6. ResponseExceptionMapper リンクのコピーリンクがクリップボードにコピーされました!
org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper クラスは、Jakarta RESTful Web Services で定義される javax.ws.rs.ext.ExceptionMapper クラスのクライアント側の逆です。つまり、ExceptionMapper.toResponse () メソッドは、サーバー側の処理中に出力された Exception クラスを Response に変えるため、ResponseExceptionMapper.toThrowable() メソッドは HTTP エラーステータスとともにクライアント側で受信された Response クラスを Exception クラスに変換します。
ResponseExceptionMapper クラスは、プログラム上または宣言的に登録できます。登録された ResponseExceptionMapper クラスがない場合、デフォルトの ResponseExceptionMapper クラスはステータス >= 400 のレスポンスを WebApplicationException クラスにマップします。
2.19.7. Jakarta Contexts and Dependency Injection の統合 リンクのコピーリンクがクリップボードにコピーされました!
MicroProfile REST クライアントでは、@RegisterRestClient クラスで Jakarta Contexts and Dependency Injection Bean として管理されるインターフェイスにアノテーションを付ける必要があります。以下に例を示します。
@Path("resource")
@RegisterProvider(MyClientResponseFilter.class)
public static class TestResourceImpl {
@Inject TestDataBase db;
@Path("test/{path}")
@Consumes("text/plain")
@Produces("text/html")
@POST
public String test(@PathParam("path") String path, @QueryParam("query")
String query, String entity) {
return db.getByName(query);
}
}
@Path("database")
@RegisterRestClient
public interface TestDataBase {
@Path("")
@POST
public String getByName(String name);
}
ここで、MicroProfile REST クライアント実装は TestDataBase クラスサービスのクライアントを作成し、TestResourceImpl クラスによるアクセスを容易にします。ただし、TestDataBase クラス実装へのパスに関する情報は含まれません。この情報は、オプションの @Reg- isterProvider パラメーター baseUri で提供されます。
@Path("database")
@RegisterRestClient(baseUri="https://localhost:8080/webapp")
public interface TestDataBase {
@Path("")
@POST
public String getByName(String name);
}
これは、https://localhost:8080/webapp で TestDataBase 実装にアクセスできることを示しています。以下のシステム変数を外部に提供することもできます。
<fully qualified name of TestDataBase>/mp-rest/url=<URL>
たとえば、以下は、https://localhost:8080/webapp で com.bluemonkeydiamond.TestDatabase クラスの実装にアクセスできることを示しています。
com.bluemonkeydiamond.TestDatabase/mp-rest/url=https://localhost:8080/webapp