4.6.2. HTTP クライアントの変更点
本セクションでは、HTTP クライアントの変更点を説明します。
以下のタイプの Eclipse Vert.x クライアントを利用できます。
- Eclipse Vert.x Web クライアント
- アプリケーションが Web 指向の場合は、Eclipse Vert.x Web クライアントを使用します。たとえば、REST、HTTP ペイロードのエンコーディングおよびデコード、HTTP ステータス応答コードの解釈などです。
- Eclipse Vert.x HTTP クライアント
- アプリケーションが HTTP プロキシーとして使用される場合は、Eclipse Vert.x HTTP クライアントを使用します。たとえば、API ゲートウェイとして。Eclipse Vert.x 4 では、HTTP クライアントが更新され、改善されました。
Eclipse Vert.x Web クライアントは Eclipse Vert.x HTTP クライアントに基づいています。
4.6.2.1. アプリケーションの Eclipse Vert.x Web クライアントへの移行 リンクのコピーリンクがクリップボードにコピーされました!
Web クライアントは Eclipse Vert.x 3.4.0 リリースから入手できました。Eclipse Vert.x 4 の Web クライアントには変更はありません。
クライアントは、簡素化された HTTP 対話と、Eclipse Vert.x HTTP クライアントでは利用できない HTTP セッション、JSON エンコーディングおよびデコーディング、応答述語などの一部の追加機能を提供します。
以下の例は、Eclipse Vert.x 3.x リリースで HTTP クライアントを使用する方法を示しています。
HttpClientRequest request = client.get(80, "example.com", "/", response -> {
int statusCode = response.statusCode();
response.exceptionHandler(err -> {
// Handle connection error, for example, connection closed
});
response.bodyHandler(body -> {
// Handle body entirely
});
});
request.exceptionHandler(err -> {
// Handle connection error OR response error
});
request.end();
以下の例は、Eclipse Vert.x 3.x および Eclipse Vert.x 4 リリースの Web クライアントにアプリケーションを移行する方法を示しています。
client.get(80, "example.com", "/some-uri")
.send(ar -> {
if (ar.suceeded()) {
HttpResponse<Buffer> response = ar.result();
// Handle response
} else {
// Handle error
}
});