问题描述
我们有一个Spring项目即将投入生产.当前,该项目正在使用Apache Http Client
.有一种使用RestTemplate
作为HttpClient
的想法.
we have a Spring project that is about to go into production. Currently, the project is using Apache Http Client
. There is a thought of using RestTemplate
as HttpClient
.
我正在四处寻找使用RestTemplate
而不是Apache's
的任何显着优势.另外,知道哪种HTTP传输在其实现中执行RestTemplate也会很有趣. Apache Http Client已被多个小组使用了多年,并享有良好的声誉.
I am digging around to see any notable advantage of using RestTemplate
over Apache's
. Also, it would be interesting to know what HTTP transport does RestTemplate in its implementation. Apache Http Client has been used by several groups for many years and has a good reputation.
我们会冒险搬到RestTemplate
吗?
此外,此博客指出,必须将RestTemplate配置用于生产,尽管配置很小.
Further, this blog points that RestTemplate needs to be configured for production, although the configuration is minimal.
谢谢
推荐答案
RestTemplate和HttpClient不在同一抽象级别上运行.
RestTemplate and HttpClient don't operate at the same abstraction level.
HttpClient是使用HTTP进行通信的通用库,而RestTemplate是用于处理实体的JSON/XML转换等的更高级别的抽象.
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 和使用Apache的HttpClient.
RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.
因此,如果目标是与Restful API通信,而您仍要使用HttpClient,则可以在HttpClient上使用RestTemplate.
So, if the goal is to communicate with a Restful API, and you still want to use HttpClient, you can use RestTemplate over HttpClient.
请注意,我刚才所说的正是您链接到的博客所解释的内容:
Note that what I just said is exactly what the blog you linked to explains:
这篇关于RestTemplate与Apache Http Client在Spring项目中的生产代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!