- Enclosing interface:
- HttpClientContext
public static interface HttpClientContext.Builder
Builds the HttpClientContext.
HttpClientContext ctx = HttpClientContext.newBuilder()
.withBaseUrl("http://localhost:8080")
.withRequestListener(new RequestLogger())
.withBodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
.build();
HelloDto dto = ctx.request()
.path("hello")
.queryParam("name", "Rob")
.queryParam("say", "Ki ora")
.GET()
.bean(HelloDto.class);
-
Method Summary
Modifier and Type Method Description HttpClientContext
build()
Build and return the context.HttpClientContext.Builder
with(HttpClient client)
Set the underlying HttpClient to use.HttpClientContext.Builder
withAuthTokenProvider(AuthTokenProvider authTokenProvider)
Add a Authorization token provider.HttpClientContext.Builder
withBaseUrl(String baseUrl)
Set the base URL to use for requests created from the context.HttpClientContext.Builder
withBodyAdapter(BodyAdapter adapter)
Set the body adapter to use to convert beans to body content and response content back to beans.HttpClientContext.Builder
withCookieHandler(CookieHandler cookieHandler)
Specify a cookie handler to use on the HttpClient.HttpClientContext.Builder
withExecutor(Executor executor)
Specify the Executor to use for asynchronous tasks.HttpClientContext.Builder
withRedirect(HttpClient.Redirect redirect)
Specify the redirect policy.HttpClientContext.Builder
withRequestIntercept(RequestIntercept requestIntercept)
Add a request interceptor.HttpClientContext.Builder
withRequestListener(RequestListener requestListener)
Add a request listener.HttpClientContext.Builder
withRequestTimeout(Duration requestTimeout)
Set the default request timeout.HttpClientContext.Builder
withRetryHandler(RetryHandler retryHandler)
Set a RetryHandler to use to retry requests.HttpClientContext.Builder
withVersion(HttpClient.Version version)
Specify the HTTP version.
-
Method Details
-
with
Set the underlying HttpClient to use.Used when we wish to control all options of the HttpClient.
-
withBaseUrl
Set the base URL to use for requests created from the context.Note that the base url can be replaced via
HttpClientRequest.url(String)
. -
withRequestTimeout
Set the default request timeout.- See Also:
HttpRequest.Builder.timeout(Duration)
-
withBodyAdapter
Set the body adapter to use to convert beans to body content and response content back to beans. -
withRetryHandler
Set a RetryHandler to use to retry requests. -
withRequestListener
Add a request listener. Multiple listeners may be added, when do so they will process events in the order they were added.Note that
RequestLogger
is an implementation for debug logging request/response headers and content. -
withRequestIntercept
Add a request interceptor. Multiple interceptors may be added. -
withAuthTokenProvider
Add a Authorization token provider.When set all requests are expected to use a Authorization Bearer token unless they are marked via
HttpClientRequest.skipAuthToken()
.The AuthTokenProvider obtains a new token typically with an expiry. This is automatically called as needed and the Authorization Bearer header set on all requests (not marked with skipAuthToken()).
-
withCookieHandler
Specify a cookie handler to use on the HttpClient. This would override the default cookie handler. -
withRedirect
Specify the redirect policy. Defaults to HttpClient.Redirect.NORMAL. -
withVersion
Specify the HTTP version. Defaults to not set. -
withExecutor
Specify the Executor to use for asynchronous tasks. If not specified a default executor will be used.- See Also:
HttpClient.Builder.executor(Executor)
-
build
HttpClientContext build()Build and return the context.HttpClientContext ctx = HttpClientContext.newBuilder() .withBaseUrl("http://localhost:8080") .withRequestListener(new RequestLogger()) .withBodyAdapter(new JacksonBodyAdapter(new ObjectMapper())) .build(); HelloDto dto = ctx.request() .path("hello") .queryParam("say", "Ki ora") .GET() .bean(HelloDto.class);
-