4.6.2.2.3. 応答の処理

HttpClientResponse インターフェースが更新され、以下の方法で改善されました。

body() メソッド

Body () メソッドは非同期バッファーを返します。Body Handler () の代わりに body() メソッドを使用します。

以下の例は、bodyHandler() メソッドを使用して要求ボディーを取得する方法を示しています。

response.bodyHandler(body -> {
  // Process the request body
});
response.exceptionHandler(err -> {
  // Could not get the request body
});

以下の例は、body() メソッドを使用してリクエストボディーを取得する方法を示しています。

response.body(ar -> {
  if (ar.succeeded()) {
    // Process the request body
  } else {
    // Could not get the request body
  }
});
end() method

End() メソッドは、応答が正常にまたは失敗した場合に future を返します。このメソッドは応答ボディーを削除します。End Handler()メソッドの代わりに このメソッドを使用してください。

以下の例は、endHandler() メソッドを使用する方法を示しています。

response.endHandler(v -> {
  // Response ended
});
response.exceptionHandler(err -> {
  // Response failed, something went wrong
});

以下の例は、end() メソッドの使用方法を示しています。

response.end(ar -> {
  if (ar.succeeded()) {
    // Response ended
  } else {
    // Response failed, something went wrong
  }
});

OnSucces ()compose()、bodyHandler() などのメソッドで応答を処理することもできます。以下の例は、on Success() メソッドを使用して応答を処理する方法を示しています。

以下の例は、Eclipse Vert.x 3.x リリースで result() メソッドで HTTP クライアントを使用する方法を示しています。

HttpClient client = vertx.createHttpClient(options);

    client.request(HttpMethod.GET, 8443, "localhost", "/")
      .onSuccess(request -> {
        request.onSuccess(resp -> {

        //Code to handle HTTP response
        });
      });

以下の例は、Eclipse Vert.x 4 の result() メソッドで HTTP クライアントを使用する方法を示しています。

HttpClient client = vertx.createHttpClient(options);

    client.request(HttpMethod.GET, 8443, "localhost", "/")
      .onSuccess(request -> {
        request.response().onSuccess(resp -> {

        //Code to handle HTTP response
        });
      });