Helidon WebClient

David Král
Mar 23 · 3 min read

The new Helidon WebClient for Helidon SE 2.0 allows you to perform any HTTP requests to the target endpoint (via GET, PUT, etc.), and handle the provided response in a reactive way. The WebClient’s reactive approach means that you are no longer blocked while waiting for the data.

The pre-release version of the Helidon WebClient is included in the 2.0.0-M2 release. You can download it from Maven Central Repository, or by including the following dependency in your project’s pom.xml.

<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
<version>2.0.0-M2</version>
</dependency>

Note: Pre-release versions are experimental and not intended for production use. APIs and features are not yet fully tested and are subject to change.

Features Overview

The Helidon 2.0 WebClient includes the following features:

  • Reactive approach
    Every executed request and response is handled in a reactive way. You will no longer have to wait for the server response. When the response is received, the client requests only the amount of data it can handle at that time. Therefore, the memory is not overflowed.
  • Simple, builder-like setup and execution
    Every client and request is created by a builder pattern, so it improves readability and code maintenance.
  • Following redirects
    The WebClient is able to follow the redirect chain and perform requests on the correct endpoint for you. You no longer have to point your client to the correct/final endpoint.
  • Tracing, metrics and security propagation
    When you configure the Helidon WebServer to use tracing, metrics and security, the settings are automatically propagated to the WebClient and used during request/response.

The Helidon WebClient is designed with simplicity in mind. The example below demonstrates a simple GET request to ‘target’ endpoint:

WebClient client = WebClient.builder()
.baseUri("http://localhost")
.build();

CompletionStage<String> response = webClient.get()
.path("/endpoint")
.request(String.class);

In this next example you can see that JsonObject is now being used as the return type. Since JsonObject processing is not present in the WebClient by default, it needs to be registered to the request builder using the register method as shown below:

JsonProcessing jsonProcessing = JsonProcessing.create();
WebClient client = WebClient.builder()
.baseUri("http://localhost")
.build();

CompletionStage<JsonObject> response = webClient.get()
.path("/endpoint")
.register(jsonProcessing.newReader())
.request(JsonObject.class);

You can now make requests to the endpoint using various methods such as GET or PUT.

JsonProcessing jsonProcessing = JsonProcessing.create();
JsonObject entity = //some JsonObject entity
WebClient client = WebClient.builder()
.baseUri("http://localhost")
.build();

CompletionStage<WebClientResponse> response = webClient.put()
.path("/endpoint")
.register(jsonProcessing.newWriter())
.submit(entity);

The Helidon WebClient is also highly configurable. While the default configuration is suitable for most common use cases, you can also configure it to handle your specific requirements as shown in the examples below:

Config config = Config.create();
WebClient client = WebClient.builder()
.baseUri("http://localhost")
.config(config.get("client"))
.build();

Or, using Helidon configuration such as yaml file:

client:
connect-timeout-millis: 2000
read-timeout-millis: 2000
follow-redirects: true
max-redirects: 5
headers:
- name: "Accept"
value: ["application/json","text/plain"]
services:
exclude: ["some.webclient.service.Provider"]
config:
metrics:
- methods: ["PUT", "POST", "DELETE"]
type: COUNTER
name-format: "client.counter.%1$s.%2$s"

This is a simplified configuration for the example. You can find a full version of the config here.

Samples

A sample project that shows you how to use the WebClient can be found in the Helidon repository. These samples will give you a better understanding of how to use WebClient.

Summary

Helidon WebClient is still an experimental feature. Before making it final we need your feedback. Please try it and let us know how we can improve it by leaving a comment to this article, as an issue in our issues tracker or in our official Slack channel.

Helidon

The official project Helidon blog containing articles from…

David Král

Written by

Software Developer at Oracle. Member of Helidon and Yasson team.

Helidon

Helidon

The official project Helidon blog containing articles from Helidon developers and the developers community. All articles are approved by the Helidon team. Contact @dkornilov to publish your story.

More From Medium

More on Helidon from Helidon

More on Helidon from Helidon

Introducing the Helidon CLI

More on Helidon from Helidon

More on Helidon from Helidon

Reactive Messaging with Helidon 2.0

Mar 25 · 4 min read

78

More on Java from Helidon

More on Java from Helidon

Helidon and JPA

Mar 2 · 12 min read

76

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade