WebClient vs RestTemplate
Asked Answered
T

6

96

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?

Tameshatamez answered 26/12, 2017 at 6:32 Comment(1)
No. RestTemplate keeps existing. WebClient is preferrable in these scenarios, i.e. when you want a reactive web client (asynchronous, non-blocking, using Flux/Mono).Cyclopropane
D
126

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

Damal answered 26/12, 2017 at 7:33 Comment(4)
Can you please check latest on this? It says, its deprecatedQuass
@pramod what is deprecated?Damal
docs.spring.io/spring/docs/current/javadoc-api/org/… here it says rest template will be depreciated In future versionQuass
Spring 5.0 docs said RestTemplate would become deprecated, Spring 5.2 however softened that, it says maintenance mode.Scotney
A
59

According to the Java Doc the RestTemplate will be in maintenance mode. Spring team advise to use the WebClient if possible:

NOTE: As of 5.0, the non-blocking, reactive org.springframework.web.reactive.client.WebClient offers a modern alternative to the RestTemplate with efficient support for both sync and async, as well as streaming scenarios. The RestTemplate will be deprecated in a future version and will not have major new features added going forward.

Anlace answered 10/9, 2018 at 7:52 Comment(3)
So now which one is correct answer? We really don't want to migrate which is deprecatedQuass
docs.spring.io/spring/docs/current/javadoc-api/org/… Here it says its going to be deprecated in future versionsQuass
It's been said above, but they no longer call it "deprecated": "NOTE: As of 5.0 this class is in maintenance mode, with only minor requests for changes and bugs to be accepted going forward. Please, consider using the org.springframework.web.reactive.client.WebClient which has a more modern API and supports sync, async, and streaming scenarios."Marris
A
8

As per the announcement, from Spring 6.1 and Spring Boot 3.2 we have a brand new option called RestClient:

Spring Framework 6.1 M2 introduces the RestClient, a new synchronous HTTP client. As the name suggests, RestClient offers the fluent API of WebClient with the infrastructure of RestTemplate.

RestClient restClient = RestClient.create();

String result = restClient.get()
  .uri("https://example.com")
  .retrieve()
  .body(String.class);

Based on the JavaDoc of RestTemplate, this is now the recommended replacement:

NOTE: As of 6.1, RestClient offers a more modern API for synchronous HTTP access.

The new RestClient can also be used with the recently introduced declarative HTTP interface without including Webflux on the classpath.

RestClient restClient = RestClient.builder().baseUrl("https://api.github.com/").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

RepositoryService service = factory.createClient(RepositoryService.class);
Allegra answered 22/8, 2023 at 17:43 Comment(0)
A
6

RestTemplate is not really deprecated. But it will not be evolved in the future. So sticking to RestTemplate is perfectly valid if it does what you need.

Another way to put that is that if you need specific usage patterns like streaming, scatter/gatter, or custom timeouts, this won't be covered by RestTemplate and you need to use WebClient instead.

Now using WebClient in a blocking application is fine too. Using block() shouldn't hurt there and Spring MVC controller does partially support reactive return types.

Arly answered 12/4, 2021 at 10:22 Comment(0)
F
5

WebClient is Non-Blocking Client, RestTemplate is Blocking Client.

For a long time, spring serves as a web customer. Under the hood, RestTemplate uses the Java API API, which is based on the subject model.This means that the matter will be blocked until the client receives a response. The problem with the blockage code is due to the existence of any string of memory and cpu cycles. Let us consider a lot of applications that are waiting for low services that are needed to produce the result.Sooner or later, requests for the results are collected. As a result, the program creates many issues, which result in depletion of a pool of thread or occupying all of the available memory. We can also experience performance performance due to the cpu switching.

Spring WebClient vs. RestTemplate

Financier answered 16/10, 2019 at 12:37 Comment(1)
"Java API API, which is based on the subject model" - this makes no sense to me. Some correction needed? Also: "that the matter will be blocked" - do you mean the thread? There are many errors in this - I would recommend shortening or deleting this answer.Immerge
M
3

WebClient supports asynchronous as well as synchronous calls. RestTemplate supports only synchronous calls. No changes are required in old code even if the RestTemplate is depracated(as long as you don't need asynchronous behaviour)

Mighty answered 24/12, 2019 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.