In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "RestTemplate how to achieve a variety of underlying HTTP client class library switching" article, most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "RestTemplate how to achieve a variety of underlying HTTP client class library switching" article bar.
First, source code analysis
RestTemplate has a very important class called HttpAccessor, which can be understood as the basic class for HTTP contact access. The following figure shows the source code:
We can analyze the following information from the source code
RestTemplate supports at least three HTTP client libraries.
SimpleClientHttpRequestFactory . The corresponding HTTP library is HttpURLConnection that comes with java JDK.
HttpComponentsAsyncClientHttpRequestFactory . The corresponding HTTP library is Apache HttpComponents.
OkHttp3ClientHttpRequestFactory . The corresponding HTTP library is OkHttp
HttpURLConnection that comes with java JDK is the default underlying HTTP implementation client.
SimpleClientHttpRequestFactory, that is, the HttpURLConnection included with java JDK does not support the Patch method of HTTP protocol. If you want to use the Patch method, you need to switch the underlying HTTP client implementation to Apache HttpComponents or OkHttp.
You can toggle RestTemplate's underlying HTTP client implementation class library by setting the setRequestFactory method.
Second, the bottom layer realizes the switching method
From the feedback of developers and the evaluation of the performance and ease of use of various HTTP clients on the Internet, OkHttp is better than Apache HttpComponents and Apache HttpComponents is better than HttpURLConnection. So I personally suggest that you switch the underlying HTTP implementation to okHTTP.
The following switching methods are based on the first article: focus on RestTemplate part 1-how to use it in Spring or non-Spring environments
2.1. Switch to okHTTP
First, the package of okHTTP is introduced into the project through maven coordinates.
Com.squareup.okhttp3 okhttp 4.7.2
If you are in a spring environment, initialize the RestTemplate bean object using OkHttp3ClientHttpRequestFactory as follows.
@ Configurationpublic class ContextConfig {@ Bean ("OKHttp3") public RestTemplate OKHttp3RestTemplate () {RestTemplate restTemplate = new RestTemplate (new OkHttp3ClientHttpRequestFactory ()); return restTemplate;}}
If you are in a non-Spring environment, you can use it directly after new RestTemplate (new OkHttp3ClientHttpRequestFactory ()).
2.2. Switch to Apache HttpComponents
Similar to the method of switching to okHTTP, I will not repeat it.
Org.apache.httpcomponents httpclient 4.5.12
Initialize the RestTemplate bean object using HttpComponentsClientHttpRequestFactory
@ Bean ("httpClient") public RestTemplate httpClientRestTemplate () {RestTemplate restTemplate = new RestTemplate (new HttpComponentsClientHttpRequestFactory ()); return restTemplate;} above is the content of this article on "how to switch between multiple underlying HTTP client libraries in RestTemplate". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.