RestTemplate vs Apache Http Client for production code in spring project

后端 未结 1 1123
天命终不由人
天命终不由人 2020-12-04 19:21

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

相关标签:
1条回答
  • 2020-12-04 19:52

    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.

    0 讨论(0)
提交回复
热议问题