Interface HttpClient
-
- All Known Implementing Classes:
NewHttpClientImpl
public interface HttpClientThe central interface for the execution of HTTP requests.
An object implementing this interface allows the execution of asynchronous HTTP requests. The interface supports lambda expressions for defining the requests to be executed and consuming the responses. Requests are defined by
Consumerobjects, which are invoked with a concreteRequestBuilderto generate a proper request representation. This allows for a declarative approach when specifying the properties of requests as shown in the following example:httpClient.execute(builder -> builder.method(RequestBuilder.Method.POST) .uri(endpointUri()) .body(body -> body.string(CONTENT, CONTENT_TEXT_PLAIN)), ...);When the response arrives it is passed to a
ResponseProcessor, which can evaluate the data and generate a corresponding result object. Again, a lambda expression can be provided to process the response.Implementations of this interface are expected to be thread-safe. They are usually stored centrally and shared by multiple components.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> CompletableFuture<T>execute(Consumer<? super RequestBuilder> producer, ResponseProcessor<? extends T> processor)Executes an HTTP request asynchronously and returns a future object with the result.
-
-
-
Method Detail
-
execute
<T> CompletableFuture<T> execute(Consumer<? super RequestBuilder> producer, ResponseProcessor<? extends T> processor)
Executes an HTTP request asynchronously and returns a future object with the result. The method first invokes theConsumerto generate the request to be executed. This request is then sent to the server. If sending fails, e.g. because no connection could be established, the resulting future is failed with the corresponding exception. Otherwise, theResponseProcessoris invoked with a representation of the response; the outcome of this object is then used to complete the result future.- Type Parameters:
T- the type of the result produced by theResponseProcessor- Parameters:
producer- the object to produce the requestprocessor- the object to process the response- Returns:
- a future with the result of the execution
-
-