本文介绍了WebClient与RestTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从春季5开始:

它已被创建为Spring Web Reactive模块的一部分,并将在这些情况下替代经典的RestTemplate.新的客户端是一种可响应的,非阻塞的解决方案,可通过HTTP/1.1协议进行工作

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

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

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?

推荐答案

否,RestTemplate将继续存在(至少目前如此).您不必将其替换为WebClient.
RestTemplate的主要区别之一是同步和阻止.也就是说,当您拨打休息电话时,您需要等到响应返回后才能继续进行.

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.

但是WebClient与此完全相反.呼叫者不必等到响应返回.相反,当有回应时,他会被通知.

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.

如果您需要这样的功能,那么是的,您需要将Resttemplate替换为WebClient.
您实际上可以使用.block()在Webclient中实现类似同步处理的Rest模板.但是另一种方式是不可能的.

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.

RestTemplate将在以后的版本(> 5.0)中弃用,并且以后将不再添加主要的新功能

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

这篇关于WebClient与RestTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:12