RestTemplate vs Apache Http Client for production code in spring project

百般思念 提交于 2019-11-27 01:08:57

问题


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.

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.

would we be risking moving to RestTemplate?

Further, this blog points that RestTemplate needs to be configured for production, although the configuration is minimal.

Thanks


回答1:


RestTemplate and HttpClient don't operate at the same abstraction level.

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 delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.

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:

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.



来源:https://stackoverflow.com/questions/31483874/resttemplate-vs-apache-http-client-for-production-code-in-spring-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!