Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient vs RestTemplate

As per spring 5:

WebClient is an interface representing the main entry point for performing web requests.

It has been created as a part of the Spring Web Reactive module and will be replacing the classic RestTemplate in these scenarios. The new client is a reactive, non-blocking solution that works over the HTTP/1.1 protocol

Does that mean, we need to recode for the old applications using RestTemplate if we want to upgrade to Spring 5?

Or there is some workaround to work with RestTemplate in Spring 5?

like image 719
KayV Avatar asked Dec 26 '17 06:12

KayV


People also ask

Which is better RestTemplate or WebClient?

Compared to 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.

Is RestTemplate going away?

The RestTemplate will be deprecated in a future version and will not have major new features added going forward. Let's see how we can use WebClient to make calls to other rest services and how it's better than RestTemplate.

What is the difference between RestTemplate and HTTP client?

HttpClient is a general-purpose library to communicate using HTTP, whereas RestTemplate is a higher-level abstraction, dealing with JSON/XML transformation of entities, etc. RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.

Is WebClient thread-safe Java?

Because WebClient is immutable it is thread-safe.


1 Answers

No, RestTemplate will continue to exist (at least for now). You don't have to replace it with WebClient.
One of the main differences is RestTemplate is synchronous and blocking i.e. when you do a rest call you need to wait till the response comes back to proceed further.

But WebClient is complete opposite of this. The caller need not wait till response comes back. Instead he will be notified when there is a response.

If you need such a functionality, then yes you need to replace your Resttemplate with WebClient.
You can in fact achieve Rest template like synchronous processing in webclient using .block(). But the other way is not possible.

EDIT:

RestTemplate will be deprecated in a future version(> 5.0) and will not have major new features added going forward

like image 129
pvpkiran Avatar answered Sep 18 '22 17:09

pvpkiran