Consume Reactive Web Service using WebClient of Spring WebFlux

WebClient is an interface, implementation is the DefaultWebClient object, introduced with Spring WebFlux that allows us to consume the Reactive Web Service. How to create a request and handle a response using the WebClient? Let’s find out in this tutorial.

First, I will create a new Spring Boot project with Spring WebFlux dependency as an example:

Consume Reactive Web Service using WebClient of Spring WebFlux

Result:

Consume Reactive Web Service using WebClient of Spring WebFlux

As an example of Reactive Web Service, I will use the example I created in Reactive web using annotation with Spring WebFlux:

Consume Reactive Web Service using WebClient of Spring WebFlux

And for simplicity, I will code to run Java console application with Spring Boot by implementing the CommandLineRunner interface like this:

Since our Spring Boot project has a dependency declaration for Spring WebFlux, our application will start using the default port of 8080. As the Reactive Web Service is running on port 8080, so I will change the port for our example in this tutorial using port 8081. We just need declare the server.port=8081 property in the application.properties file.

OK, everything is ready, now let’s go to the main topic of this tutorial!




I will declare the base URL that our example will consume as follows:

To use WebClient object to consume Reactive Web Service above, we first need to initialize the object for it.

There are 3 ways to do this:

The first is that we will create the WebClient object with the default configuration as follows:

The second way is to create a new WebClient object with the request URL that we need to consume:

Finally, we can use the DefaultWebClientBuilder object to initialize the WebClient object with more information:

Here, I just initialized the WebClient object using the DefaultWebClientBuilder object with the simple information of baseUrl. You can configure more information, such as:

Consume Reactive Web Service using WebClient of Spring WebFlux

In this tutorial, I will use the 3rd way!

Once you have the WebClient object, you can use it to build the request, send the request and receive the response.

WebClient supports us all HTTP methods, so we can easily build any request.

You can use the get(), post(), put(), patch(), delete() methods to build GET, POST, PUT, PATCH, DELETE requests.

Alternatively, you can use the method() method with the enum HttpMethod parameter to do this:

Once you’ve defined the HTTP method, you need to define the URI that you need to request. In my example, this definition would be:

Next, depending on the request, you need to build the body data for the request. GET request is not required but POST request will be needed. In the case of POST, for body data we will use the body() method, for example:

Here we have used the abstract class BodyInserters to populate data to the body of the request. This class supports many different ways to populate different types of data. You can find out more!

In this tutorial, my request is a GET request, so my code is as simple as:

Once the request has been completed, we will now start sending the request and receive the response.

To do this, you can use the exchange() or retrieve() method. The difference between these two methods is mainly in the type of data they return.

The exchange() method returns a ClientResponse object with HTTP status and header information. We must use the block() method to get data in the body of the response.

The retrieve() method will return the ResponseSpec object so we can get the data in the body of the response directly:

As you can see, we will use the bodyToFlux() or bodyToMono() method to get the data in the body of the response. These methods will throw a WebClientResponseException if the HTTP Status returned is 4xx (error by client) or 5xx (server error).

Once you have flux or mono from the response body, you can use these objects to retrieve the data you need.

Get the first data when our example connects to the Reactive Web Service:

Result:

Consume Reactive Web Service using WebClient of Spring WebFlux

If you want to keep subscribing to receive data when new data is added, you can code as follows:

Result:

Consume Reactive Web Service using WebClient of Spring WebFlux

Consume Reactive Web Service using WebClient of Spring WebFlux
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
2Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.