RestTemplate与Apache Http Client在Spring项目中的生产代码 [英] RestTemplate vs Apache Http Client for production code in spring project

查看:120
本文介绍了RestTemplate与Apache Http Client在Spring项目中的生产代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个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:

因此,解决方案是使用org.springframework.http.client.HttpComponentsClientHttpRequestFactory,这是一个ClientHttpRequestFactory,将请求的创建委托给HttpClient.

So, the solution is to use the org.springframework.http.client.HttpComponentsClientHttpRequestFactory, which is a ClientHttpRequestFactory delegating the creation of the requests to an HttpClient.

这篇关于RestTemplate与Apache Http Client在Spring项目中的生产代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆