Reactive WebClient
Spring WebFlux provides the reactive WebClient to make asynchronous api calls. It is fully non-blocking. Things I like about WebClient is it integrates well with reactor api providing very readable code and it is configurable in every way, which we will see in a minute.
Let’s start with creating an instance of the WebClient. There are two ways to do so:
- Using WebCient.create() or WebClient.builder() functions
- Autowiring the WebClient.Builder Bean which spring provides
Once created, the WebClient is immutable which makes it thread safe. You can use webClient.mutate() to reconfigure it and get a new instance.
As we have created our WebClient, now let’s make an api call.
That’s all. Only with four lines of code we are able to make a GET api call. Let’s break down the code and look at individual parts
- get() method specifies that it’s a GET api call
- uri() takes the uri string. You can also use another flavour of uri() which takes a function allowing you to configure the uri using UriBuilder
- retrieve() method is used to retrieve the response body.
- bodyToMono(Customer::class.java) is really interesting. It uses the Spring Codecs to deserialize the data into required domain class. More on Codecs; later in this blog
Similar way, we can make the following post api call.
The only difference here is bodyValue() method, which we use to send the request body
Let’s look at some of the configuration which we can do to the WebClient.
One of the thing which we followed in our project and I find it very useful is: having the configuration of WebClient at one place in a starter/library and using it across all micro-services. This instance will have everything configured: Default Headers, Filters, Codecs, Error Handling etc.
Default Headers
We can configure the default headers for every request which goes from our application
Filters
You can create and plugin custom filters into the WebClient to intercept the request and/or response. This is helpful for handling cross-cutting concerns like authentication or logging. Below is an example of such a filter:
To plugin this filter into the WebClient, we can use the filter() method provided by WebClient builder
WebClient Codecs
A Codec is the guy who takes care of encoding/decoding the data going/coming from the network call. WebClient makes use of spring codecs for this purpose so we don’t have to worry about all these.
But still, in certain cases where we have to use a custom codec; WebClient allows us to configure the custom codecs as show below
You can create a CustomCodec by extending the HttpMessageReader
Some more on Codecs
By default, web client codecs can buffer 256 KB of data into memory. So if you get an api response of size more than 256 KB you will get a DataBufferLimitException
You can change this limit by modifying the default codecs’ settings as follows:
You can also configure this through application properties by setting spring.codec.max-in-memore-size as follows
spring:
codec:
max-in-memory-size: 1048576
But for this to work you will have to use the WebClient.Builder bean provided by the spring boot.
References:
Documentation of Spring Webflux
If you like this blog, please clap and/or leave response below
Follow me for more upcoming blogs on Kotlin, Spring Webflux, Reactor and much more…