spring WebClient 与 RestTemplate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47974757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:33:39  来源:igfitidea点击:

WebClient vs RestTemplate

springreactive-programmingresttemplateweb-client

提问by KayV

As per spring 5:

根据春季 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

WebClient 是一个接口,代表执行 Web 请求的主要入口点。

它已作为 Spring Web Reactive 模块的一部分创建,并将在这些场景中替换经典的 RestTemplate。新客户端是一种基于 HTTP/1.1 协议的反应式非阻塞解决方案

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

这是否意味着,如果我们想升级到 Spring 5,我们需要使用 RestTemplate 为旧应用程序重新编码?

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

或者有一些解决方法可以在 Spring 5 中使用 RestTemplate 吗?

回答by pvpkiran

No, RestTemplate will continue to exist(atleast for now). You don't have to replace it with WebClient.
One of the main difference 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.

不,RestTemplate 将继续存在(至少现在)。您不必用 WebClient 替换它。
主要区别之一是 RestTemplate 是同步的和阻塞的。即,当您进行休息呼叫时,您需要等到响应返回才能进一步进行。

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.

但是 WebClient 与此完全相反。调用者无需等到响应返回。相反,当有响应时,他会收到通知。

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

如果您需要这样的功能,那么是的,您需要用 WebClient 替换您的 Resttemplate。
实际上,您可以使用.block(). 但另一种方式是不可能的。

EDIT:

编辑:

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

RestTemplate 将在未来版本(> 5.0)中被弃用,并且不会在未来添加主要的新功能

回答by Evgeni Dimitrov

According to the Java Docthe RestTemplate will be deprecated. Spring team advise to use the WebClient if possible:

根据Java DocRestTemplate 将被弃用。Spring 团队建议尽可能使用 WebClient:

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.

注意:从 5.0 开始,非阻塞、反应式 org.springframework.web.reactive.client.WebClient 提供了 RestTemplate 的现代替代方案,有效支持同步和异步以及流场景。RestTemplate 将在未来版本中被弃用,并且不会在未来添加主要的新功能。

回答by psl

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)

WebClient 支持异步和同步调用。RestTemplate 仅支持同步调用。即使 RestTemplate 已弃用,旧代码也不需要更改(只要您不需要异步行为)

回答by ismael

WebClient is Non-BlockingClient, RestTemplate is BlockingClient.

WebClient 是非阻塞客户端,RestTemplate 是阻塞客户端。

For a long time, spring serves as a web customer. Under the hood, RestTemplateuses 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 都是 web 客户。在底层RestTemplate使用基于主题模型的 Java API API。这意味着事件将被阻止,直到客户端收到响应。阻塞代码的问题是由于内存和cpu周期的任何字符串的存在。让我们考虑许多正在等待生成结果所需的低服务的应用程序。迟早会收集对结果的请求。因此,程序会产生许多问题,导致线程池耗尽或占用所有可用内存。我们还可以体验到 CPU 切换带来的性能表现。

Spring WebClient vs. RestTemplate

Spring WebClient 与 RestTemplate