Spring 5 documentation suggests that WebClient is now the preferred way to make HTTP requests.
WebClient is part of Spring WebFlux and is intended to replace the classic RestTemplate.
WebClient has a more functional feel and is fully reactive. Since Spring 5.0, RestTemplate is deprecated. It will probably stay for some more time but will not have major new features added going forward in future releases. So it’s not advised to use
RestTemplate in new code.
1. Difference between WebClient and RestTemplate
1.1. RestTemplate is blocking
RestTemplate are blocking in nature and uses one thread-per-request model of Java Servlet API. It means that
RestTemplate will wait for the response everytime it dispatches a request to the remote server.
RestTemplate creates new
Httpconnection every time and closes the connection once response is received and processed.
Creating and closing the URL connections is a costly operation. For using
RestTemplate in our production class applications, we must use a HTTP connection pooling.
Please note that RestTemplate is thread-safe and a single instance can be shared across multiple connections at any time.
For each request,
RestTemplate craetes a new
Thread and uses it for request-response lifecyle. After sending a request, RestTemplate waits for the response from the server until a configured timeout is reached. This action blocks the thread.
When there are high number of requests in the application, there will be high number of threads and connections, in proportion. This puts a load on the server resources.
And if the server is slow, soon, users will start seeing the degraded performance and even unresponsiveness in the application.
1.2. WebClient is non-blocking
Opposite to RestTemplate,
WebClient is asynchronous and non-blocking in nature. It follows events-diven architecture from reactive framework of Spring WebFlux.
WebClient, the client need not wait till response comes back. Instead it will be notified usign a callback method when there is a response from the server.
When we invoke an API through
WebClient that returns a
Mono or a
Flux, it will return immediately. The results of the call will be delivered to us through the mono or flux callbacks when they become available.
Please note that we can achieve
RestTemplate like synchronous processing in WebClient using
From the above discussion, it is clear that the only big difference between WebClient and RestTemplate is their blocking nature. RestTemplate blocks the request threads while WebClient does not.
We can use WebClient to make synchronous requests, but the opposite is not true. RestTemplate cannot make asynchronous requests.
While WebClient is the preferred way for future uses, RestTemplate seems to stay here for long though without any major feature addition.
While considering WebClient for our next application, we must remember that to build a truly non-blocking application, we must aim to create/use all of its components as non-blocking i.e. client, controller, middle services, and even the database. If one of them is blocking the requests, our aim will be defeated.
Happy Learning !!
Leave a Reply