Contents
Overview
WebClient is an HTTP client for Helidon SE. It can be used to send requests and retrieve corresponding responses in a programmatic way.
Helidon WebClient provides the following features:
Blocking approach
The Webclient uses the blocking approach to synchronously process a request and its correspond response. BothHTTP/1.1andHTTP/2request and response will run in the thread of the user. Additionally, forHTTP/2, virtual thread is employed to manage the connection.Builder-like setup and execution
Creates every client and request as a builder pattern. This improves readability and code maintenance.Redirect chain
Follows the redirect chain and perform requests on the correct endpoint by itself.Tracing and security propagation
Automatically propagates the configured tracing and security settings of the Helidon WebServer to the WebClient and uses them during request and response.
Maven Coordinates
To enable WebClient, add the following dependency to your project’s pom.xml (see Managing Dependencies).
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
</dependency>The helidon-webclient dependency has built-in support for HTTP/1.1.
If support for HTTP/2 is a requirement, below dependency needs to be added:
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-http2</artifactId>
</dependency>Usage
Instantiating the WebClient
You can create an instance of a WebClient by executing WebClient.create() which will have default settings and without a base uri set.
To change the default settings and register additional services, you can use simple builder that allows you to customize the client behavior.
WebClient client = WebClient.builder()
.baseUri("http://localhost")
.build();Creating the Request
WebClient offers a set of request methods that are used to specify the type of action to be performed on a given resource. Below are some examples of request methods:
get()post()put()method(Method method)
Check out HttpClient API to learn more about request methods. These methods will create a new instance of HttpClientRequest which can then be configured to add optional settings that will customize the behavior of the request.
Customizing the Request
Configuration can be set for every request type before it is sent.
client.get()
.uri("http://example.com")
.path("/path")
.queryParam("query", "parameter")
.fragment("someFragment")
.headers(headers -> headers.accept(MediaTypes.APPLICATION_JSON)); - Overrides
baseUrifrom WebClient - Adds path to the uri
- Adds query parameter to the request
- Adds fragment to the request
- Adds header to the request
For more information about these optional parameters, check out ClientRequestBase API, which is a parent class of HttpClientRequest.
HttpClientRequest class also provides specific header methods that help the user to set a particular header. Some examples of these are:
contentType(MediaType contentType)accept(MediaType… mediaTypes)
For more information about these methods, check out ClientRequest API, which is a parent class of HttpClientRequest.
Sending the Request
Once the request setup is completed, the following methods can be used to send it:
HttpClientResponse request()<E> ClientResponseTyped<E> request(Class<E> type)<E> E requestEntity(Class<E> type)HttpClientResponse submit(Object entity)<T> ClientResponseTyped<T> submit(Object entity, Class<T> requestedType)HttpClientResponse outputStream(OutputStreamHandler outputStreamConsumer)<T> ClientResponseTyped<T> outputStream(OutputStreamHandler outputStreamConsumer, Class<T> requestedType)
Each of the methods will provide a way to allow response to be retrieved in a particular response type. Refer to ClientRequest API for more details about these methods.
ClientResponseTyped<String> response = client.get()
.path("/endpoint")
.request(String.class);
String entityString = response.entity();Protocol Used
WebClient currently supports HTTP/1.1 and HTTP/2 protocols. Below are the rules on which specific protocol will be used:
Using plain socket triggers WebClient to process a request using
HTTP/1.1.When using TLS, the client will use ALPN (protocol negotiation) to use appropriate HTTP version (either 1.1, or 2).
HTTP/2has a higher weight, so it is chosen if supported by both sides.A specific protocol can be explicitly selected by calling
HttpClientRequest#protocolId(String).
String result = client.get()
.protocolId("http/1.1")
.requestEntity(String.class);If
HTTP/2is used, an upgrade attempt will be performed. If it fails, the client falls-back toHTTP/1.1.The parameter
prior-knowledgecan be defined usingHTTP/2protocol configuration. Please refer to on how to customizeHTTP/2. In such a case,prior-knowledgewill be used and fail if it is unable to switch toHTTP/2.
Adding Media Support
Webclient supports the following built-in Helidon Media Support libraries:
- JSON Processing (JSON-P)
- JSON Binding (JSON-B)
- Jackson
They can be activated by adding their corresponding libraries into the classpath. This can simply be done by adding their corresponding dependencies.
<dependency>
<groupId>io.helidon.http.media</groupId>
<artifactId>helidon-http-media-jsonp</artifactId>
</dependency><dependency>
<groupId>io.helidon.http.media</groupId>
<artifactId>helidon-http-media-jsonb</artifactId>
</dependency><dependency>
<groupId>io.helidon.http.media</groupId>
<artifactId>helidon-http-media-jackson</artifactId>
</dependency>Users can also create their own Custom Media Support library and make them work by following either of the approaches:
Create a Provider of the Custom Media Support and expose it via Service Loader followed by adding the Media Support library to the classpath.
Explicitly register the Custom Media Support from WebClient.
WebClient.builder()
.mediaContext(it -> it
.addMediaSupport(CustomMediaSupport.create()))
.build();- Register CustomMedia support from the WebClient.
DNS Resolving
Webclient provides three DNS resolver implementations out of the box:
Java DNS resolutionis the default.First DNS resolutionuses the first IP address from a DNS lookup. To enable this option, add below dependency:
<dependency>
<groupId>io.helidon.webclient.dns.resolver</groupId>
<artifactId>helidon-webclient-dns-resolver-first</artifactId>
</dependency>Round-Robin DNS resolutioncycles through IP addresses from a DNS lookup. To enable this option, add this dependency:
<dependency>
<groupId>io.helidon.webclient.dns.resolver</groupId>
<artifactId>helidon-webclient-dns-resolver-round-robin</artifactId>
</dependency>Configuring the WebClient
The class responsible for WebClient configuration is:
Configuration options
| Key | Kind | Type | Default Value | Description |
|---|---|---|---|---|
connect-timeout | VALUE | Duration | Connect timeout | |
follow-redirects | VALUE | Boolean | true | Whether to follow redirects |
keep-alive | VALUE | Boolean | true | Determines if connection keep alive is enabled (NOT socket keep alive, but HTTP connection keep alive, to re-use the same connection for multiple requests) |
max-redirects | VALUE | Integer | 10 | Max number of followed redirects |
properties | MAP | String | Properties configured for this client | |
protocol-configs | LIST | i.h.w.s.ProtocolConfig | Configuration of client protocols | |
protocol-configs-discover-services | VALUE | Boolean | true | Whether to enable automatic service discovery for protocol-configs |
protocol-preference | LIST | String | List of HTTP protocol IDs by order of preference | |
proxy | VALUE | i.h.w.a.Proxy | Proxy configuration to be used for requests | |
read-timeout | VALUE | Duration | Read timeout | |
tls | VALUE | i.h.c.t.Tls | TLS configuration for any TLS request from this client |
Protocol Specific Configuration
Protocol specific configuration can be set using the protocol-configs parameter. Webclient currently supports HTTP/1.1. and HTTP/2. Below are the options for each of the protocol type:
HTTP/1.1
Configuration options
HTTP/2
Configuration options
Example of a WebClient Runtime Configuration
Config config = Config.create();
WebClient client = WebClient.builder()
.baseUri("http://localhost")
.config(config.get("client"))
.build();Example of a WebClient YAML Configuration
client:
connect-timeout-millis: 2000
read-timeout-millis: 2000
follow-redirects: true
max-redirects: 5
cookie-manager:
automatic-store-enabled: true
default-cookies:
flavor3: strawberry
flavor4: raspberry
default-headers:
Accept: '"application/json", "text/plain"'
services:
metrics:
- methods: ["PUT", "POST", "DELETE"]
type: METER
name-format: "client.meter.overall"
- type: TIMER
# meter per method
name-format: "client.meter.%1$s"
- methods: ["GET"]
type: COUNTER
errors: false
name-format: "client.counter.%1$s.success"
description: "Counter of successful GET requests"
- methods: ["PUT", "POST", "DELETE"]
type: COUNTER
success: false
name-format: "wc.counter.%1$s.error"
description: "Counter of failed PUT, POST and DELETE requests"
tracing:
protocol-configs:
http_1_1:
max-header-size: 20000
validate-request-headers: true
h2:
prior-knowledge: true
proxy:
host: "hostName"
port: 80
no-proxy: ["localhost:8080", ".helidon.io", "192.168.1.1"]
tls:
trust:
keystore:
passphrase: "password"
trust-store: true
resource:
resource-path: "client.p12"- Client functional settings
- Cookie management
- Default client headers
- Client service configuration
- Protocol configuration
- Proxy configuration
- TLS configuration
Examples
Webclient with Proxy
Configure Proxy setup either programmatically or via the Helidon configuration framework.
Configuring Proxy in your code
Proxy can be set directly from WebClient builder.
Proxy proxy = Proxy.builder()
.type(Proxy.ProxyType.HTTP)
.host(PROXY_HOST)
.port(PROXY_PORT)
.build();
WebClient.builder()
.proxy(proxy)
.build();Alternative is to set proxy directly from the request via HttpClientRequest.
Proxy proxy = Proxy.create();
HttpClientResponse response = client.get("/proxiedresource")
.proxy(proxy)
.request();- Proxy instance configured using system settings (environment variables and system properties)
- Configure the proxy per client request
Configuring Proxy in the config file
Proxy can also be configured in WebClient through the application.yaml configuration file.
application.yamlclient:
proxy:
host: "hostName"
port: 80
no-proxy: ["localhost:8080", ".helidon.io", "192.168.1.1"]Then, in your application code, load the configuration from that file.
application.yaml file located on the classpathConfig config = Config.create();
WebClient.builder()
.config(config.get("client"))
.build();application.yamlis a default configuration source loaded when YAML support is on classpath, so we can just useConfig.create()- Passing the client configuration node
WebClient TLS Setup
Configure TLS either programmatically or by the Helidon configuration framework.
Configuring TLS in your code
One way to configure TLS in WebClient is in your application code as shown below.
WebClient.builder()
.tls(it -> it.trust(t -> t
.keystore(k -> k.passphrase("password")
.trustStore(true)
.keystore(r -> r.resourcePath("client.p12")))))
.build();Configuring TLS in the config file
Another way to configure TLS in WebClient is through the application.yaml configuration file.
application.yamlclient:
tls:
trust:
keystore:
passphrase: "password"
trust-store: true
resource:
resource-path: "client.p12"The passphrase value on the config file can be encrypted if stronger security is required. For more information on how secrets can be encrypted using a master password and store them in a configuration file, please see Configuration Secrets.
In the application code, load the settings from the configuration file.
application.yaml file located on the classpathConfig config = Config.create();
WebClient.builder()
.config(config.get("client"))
.build();application.yamlis a default configuration source loaded when YAML support is on classpath, so we can just useConfig.create()- Passing the client configuration node
Adding Service to WebClient
WebClient currently supports several built-in services, namely
metricstracingtelemetry(following OpenTelemetry semantic conventions)metricstracing
security.
Enabling the service
In order for a service to function, its dependencies need to be added in the application’s pom.xml. Below are examples on how to enable the built-in services:
discovery(see its documentation)pom.xml<dependency> <groupId>io.helidon.webclient</groupId> <artifactId>helidon-webclient-discovery</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>io.helidon.discovery.providers</groupId> <artifactId>helidon-discovery-providers-eureka</artifactId> <scope>runtime</scope> </dependency>content_copy- Backs the
discoveryservice with a Discovery provider based on Netflix’s Eureka
- Backs the
metricspom.xml<dependency> <groupId>io.helidon.webclient</groupId> <artifactId>helidon-webclient-metrics</artifactId> </dependency>content_copytracingpom.xml<dependency> <groupId>io.helidon.webclient</groupId> <artifactId>helidon-webclient-tracing</artifactId> </dependency>content_copytelemetry metricsandtracingpom.xml<dependencdy> <groupId>io.helidon.webclient</groupId> <artifactId>helidon-webclient-telemetry</artifactId> </dependencdy>content_copysecuritypom.xml<dependency> <groupId>io.helidon.webclient</groupId> <artifactId>helidon-webclient-security</artifactId> </dependency>content_copy
Adding a service in your code
Services can be added in WebClient as shown in the code below.
WebClientService clientService = WebClientMetrics.counter()
.methods(Method.GET)
.nameFormat("example.metric.%1$s.%2$s")
.build();
WebClient.builder()
.addService(clientService)
.build();- Creates new metric which will count all GET requests and has format of
example.metric.GET.<host-name> - Register the service in the client instance
Adding service in the config file
Adding service in WebClient can also be done through the application.yaml configuration file.
application.yamlwebclient:
services:
metrics:
- type: METER
name-format: "client.meter.overall"
- type: TIMER
# meter per method
name-format: "client.meter.%1$s"
- methods: ["PUT", "POST", "DELETE"]
type: COUNTER
success: false
name-format: "wc.counter.%1$s.error"
description: "Counter of failed PUT, POST and DELETE requests"
tracing:Then, in your application code, load the configuration from that file.
application.yaml file located on the classpathConfig config = Config.create();
WebClient.builder()
.config(config.get("client"))
.build();application.yamlis a default configuration source loaded when YAML support is on classpath, so we can just useConfig.create()- Passing the client configuration node
Setting Protocol configuration
Individual protocols can be customized using the protocol-config parameter.
Setting up protocol configuration in your code
Below is an example of customizing HTTP/1.1 protocol in the application code.
WebClient.builder()
.addProtocolConfig(Http1ClientProtocolConfig.builder()
.defaultKeepAlive(false)
.validateRequestHeaders(true)
.validateResponseHeaders(false)
.build())
.build();Setting up protocol configuration in the config file
Protocol configuration can also be set in the application.yaml configuration file.
HTTP/1.1 and HTTP/2 protocol using application.yaml file.webclient:
protocol-configs:
http_1_1:
max-header-size: 20000
validate-request-headers: true
h2:
prior-knowledge: trueThen, in your application code, load the configuration from that file.
application.yaml file located on the classpathConfig config = Config.create();
WebClient.builder()
.config(config.get("client"))
.build();application.yamlis a default configuration source loaded when YAML support is on classpath, so we can just useConfig.create()- Passing the client configuration node
Configuring Telemetry
The telemetry webclient services provide metrics and tracing spans which follow the OpenTelemetry semantic conventions for clients. These are separate from the services.metrics and services.tracing services described elsewhere on this page.
To enable the telemetry webclient services, take the following two steps:
Add the appropriate dependency.
Add configuration or code to activate the telemetry services.
To set up metrics and tracing, add the following single dependency to your project:
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-telemetry</artifactId>
<scope>runtime</scope>
</dependency>To transmit the metrics semantic conventions to a backend, add a dependency on an OpenTelemetry exporter and in the telemetry configuration set up an exporter under signals.metrics.
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<scope>runtime</scope>
</dependency>telemetry:
service: my-app
signals:
metrics:
exporters:
type: otlpTo activate webclient telemetry collection using configuration, add the telemetry config section under client.services and, below it, add metrics, tracing, or both.
client:
services:
telemetry:
metrics:
tracing:The metrics and tracing subsections have no explicit settings.
Alternatively, trigger webclient telemetry collection by modifying your client code to add one or more webclient telemetry services to the webclient builder. This example shows adding only telemetry metrics.
WebClient.builder()
.addService(WebClientTelemetryMetrics.create())
.build();Context Propagation
WebClient supports the capability to propagate values from io.helidon.common.context.Context over HTTP headers.
To enable this feature (implemented as a WebClient service), add the following dependency to your pom file:
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-context</artifactId>
</dependency>Example configuration:
client:
services:
context:
records:
# This looks up a `java.lang.String` context value classified `X-Helidon-Tid` and sends it as a `X-Helidon_Tid` header
- header: "X-Helidon-Tid"
# This looks up a `java.lang.String[]` context value classified with the classifier and sends it as a `X-Helidon-Cid` header, in case the value is not present, values "first" and "second" are sent instead
- classifier: "io.helidon.webclient.context.propagation.cid"
header: "X-Helidon-Cid"
default-value: [ "first", "second" ]
array: trueFull configuration reference: