使用HttpClient作为Spring RestTemplate的替代品。

huangapple go评论63阅读模式
英文:

Using HttpClient as an alternative of Spring RestTemplate

问题

我们正试图替换项目中的Spring RestTemplate,因为它已经过时,所以我想知道是否可以使用SAP Cloud SDK的httpClient作为Spring RestTemplate的替代品。

英文:

We are trying to replace the Spring RestTemplate from our project because its depreciated So I want to know if that is a good idea to use SAP cloud SDK httpClient as an alternative to Spring RestTemplate.

答案1

得分: 3

WebClient 是 RestTemplate 的推荐替代品。WebClient 是 Spring WebFlux 的一部分。

> 注意:从 5.0 开始,此类处于维护模式,仅接受对变更和错误的较小请求。请考虑使用具有更现代 API 并支持同步、异步和流式处理场景的 org.springframework.web.reactive.client.WebClient。

来自 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

英文:

WebClient is the recommended alternative for RestTemplate. WebClient is part of Spring WebFlux.

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

From https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

答案2

得分: 1

这取决于你的使用情况。

RestTemplate的直接后继者将是Spring的WebClient,如API注释中所述。
所以,作为直接的迁移路径,这是绝对有意义的。

如果你正在使用BTP Destinations并通过SAP Cloud SDK的DestinationAccessor来消耗它们,使用Cloud SDK公开的HttpClient也是一个不错的选择。
这将自动为你添加凭据,例如。
此外,这些目的地可以用于不涉及HttpClient的OData或OpenAPI客户端。

最终你可以选择任何一个,甚至可以组合使用(例如,Cloud SDK HttpClient用于所有与OData/OpenAPI相关的内容,WebClient用于所有手动调用)。

英文:

That depends a bit on your use case.

The direct successor of the RestTemplate would be the WebClient of Spring, as stated in the API Note.
So, as a direct migration path this makes absolute sense.

If you are using the BTP Destinations and consume them via the SAP Cloud SDK DestinationAccessor using the HttpClient exposed by the Cloud SDK would also be a good option.
This will add, for example, credentials automatically for you.
In addition these Destinations can then be used with out OData or OpenAPI clients, without ever touching the HttpClient.

In the end you can go with either of them, or even a combination (e.g. Cloud SDK HttpClient for everything OData/OpenAPI related, WebClient for all manual calls).

huangapple
  • 本文由 发表于 2023年4月19日 14:55:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定