1 restTemplate — spring 提供
特點:
1、RestOperations 提供了各種封裝方法,非常方便直接將返回轉成實體類。
2、默認使用JDK 的HttpURLConnection進行通信,但是可以通過RestTemplate.setRequestFactory 切換到不同的HTTP源:如Apache HttpComponents、Netty、OkHttp。
3、支持同步、異步請求;
4、支持更多的定制,比如攔截器等。
ps:支持 get 請求,參數是 body 的形式。
參考:https://www.huaweicloud.com/articles/7f32ca1acff12162ce8d3bace872ae04.html
國外知名博客Baeldung的博客 The Guide to RestTemplate: https://www.baeldung.com/rest-template
1.1 底層是JAVA的HttpURLConnection(默認使用,可以定制)
所有的請求都需要執行 doExecute() 方法
@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "URI is required");
Assert.notNull(method, "HttpMethod is required");
ClientHttpResponse response = null;
Object var14;
try {
// 創建請求
ClientHttpRequest request = this.createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
this.handleResponse(url, method, response);
var14 = responseExtractor != null ? responseExtractor.extractData(response) : null;
} catch (IOException var12) {
String resource = url.toString();
String query = url.getRawQuery();
resource = query != null ? resource.substring(0, resource.indexOf(63)) : resource;
throw new ResourceAccessException("I/O error on " + method.name() + " request for "" + resource + "": " + var12.getMessage(), var12);
} finally {
if (response != null) {
response.close();
}
}
return var14;
}
HttpAccessor 創建請求
public abstract class HttpAccessor {
... // 省略代碼無數
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
// 使用 ClientHttpRequestFactory 創建請求
ClientHttpRequest request = this.getRequestFactory().createRequest(url, method);
if (this.logger.isDebugEnabled()) {
this.logger.debug("HTTP " + method.name() + " " + url);
}
return request;
}
}
ClientHttpRequestFactory接口的具體實現,如:SimpleClientHttpRequestFactory 創建請求
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
// 使用 HttpURLConnection 創建請求
HttpURLConnection connection = this.openConnection(uri.toURL(), this.proxy);
this.prepareConnection(connection, httpMethod.name());
return (ClientHttpRequest)(this.bufferRequestBody ? new SimpleBufferingClientHttpRequest(connection, this.outputStreaming) : new SimpleStreamingClientHttpRequest(connection, this.chunkSize, this.outputStreaming));
}
1.2 post 請求,返回直接封裝為實體
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class);
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));
1.3 get請求,但是參數是body形式
一般 get 請求,不支持 body 傳參。
參考:https://stackoverflow.com/questions/43421126/how-to-use-httpclient-to-send-content-in-body-of-get-request/68812976#68812976
HTTP GET with a body is a somewhat unconventional construct that falls in a gray area of the HTTP specification - the end result is that many older pieces of software either cannot handle such a request at all, or will explicitly reject it because they believe it to be malformed.
帶有body參數的HTTP GET是一種非傳統的構造,屬于HTTP規范的灰色區域。最終的結果是,許多舊的軟件要么根本不能處理這樣的請求,要么會明確拒絕,因為他們認為它是格式錯誤的請求。
/**
* 注意:get請求,但是參數是body形式
*
* @param url
* @param paramBody
* @return
*/
private String getWithBody(String url, Map<String, Object> paramBody) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.AppLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity requestEntity = new HttpEntity(JsonUtil.of(paramBody), httpHeaders);
RestTemplate template = getTemplate();
ResponseEntity response = template.exchange(url, HttpMethod.GET, requestEntity, String.class);
Object result = response.getBody();
logger.info("/invokeThirdPartyRequest/getWithBody/result/[{}]", result.toString());
return result.toString();
}
/**
* 獲取 RestTemplate
*
* @return
*/
private RestTemplate getTemplate() {
RestTemplate restTemplate = new RestTemplate();
//修改restTemplate的RequestFactory使其支持Get攜帶body參數
restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
return restTemplate;
}
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import java.net.URI;
public class HttpComponentsClientRestfulHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
if (httpMethod == HttpMethod.GET) {
return new HttpGetRequestWithEntity(uri);
}
return super.createHttpUriRequest(httpMethod, uri);
}
/**
* 定義HttpGetRequestWithEntity實現HttpEntityEnclosingRequestBase抽象類,以支持GET請求攜帶body數據
*/
private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetRequestWithEntity(final URI uri) {
super.setURI(uri);
}
@Override
public String getMethod() {
return HttpMethod.GET.name();
}
}
}
2 HttpUtil — hutool 提供
HttpUtil 其實是 HttpRequest 的封裝。
它支持各種封裝好的get、post、put請求。
2.1 get 請求
public static String get(String urlString, Charset customCharset) {
return ((HttpRequest)HttpRequest.get(urlString).charset(customCharset)).execute().body();
}
public static String get(String urlString) {
return get(urlString, HttpGlobalConfig.timeout);
}
public static String get(String urlString, int timeout) {
return HttpRequest.get(urlString).timeout(timeout).execute().body();
}
// form 表單格式的入參
public static String get(String urlString, Map<String, Object> paramMap) {
return HttpRequest.get(urlString).form(paramMap).execute().body();
}
// form 表單格式的入參,并設置超時時間
public static String get(String urlString, Map<String, Object> paramMap, int timeout) {
return HttpRequest.get(urlString).form(paramMap).timeout(timeout).execute().body();
}
2.2 post 請求
這些請求最終調用的都是 HttpRequest 的 execute() 方法。
// form 表單格式的入參
public static String post(String urlString, Map<String, Object> paramMap) {
return post(urlString, paramMap, HttpGlobalConfig.timeout);
}
// form 表單格式的入參,并設置超時時間
public static String post(String urlString, Map<String, Object> paramMap, int timeout) {
return HttpRequest.post(urlString).form(paramMap).timeout(timeout).execute().body();
}
// body 格式入參
public static String post(String urlString, String body) {
return post(urlString, body, HttpGlobalConfig.timeout);
}
// body 格式入參,并設置超時時間
public static String post(String urlString, String body, int timeout) {
return HttpRequest.post(urlString).timeout(timeout).body(body).execute().body();
}
2.3 一個例子
Map<String, Object> param = new HashMap<>();
param.put("userId", userId);
String res = HttpUtil.post(url, JsonUtil.of(param));
3 HttpRequest — hutool 提供
HttpRequest 提供了非常方便構造請求的構造函數。當參數比較多、header比較多的時候,可以使用這種方式。(這里使用了構造模式)
3.1 底層是Java的HttpURLConnection
HttpRequest 底層又是使用了 java 提供的 HttpURLConnection
上源碼:
最終都需要執行這個execute方法,這個方法調用了hutool封裝的HttpConnection,這個HttpConnection又使用了java提供的HttpURLConnection。
// hutool 執行方法
public HttpResponse execute(boolean isAsync) {
this.urlWithParamIfGet();
this.initConnection();
this.send();
HttpResponse httpResponse = this.sendRedirectIfPossible();
if (null == httpResponse) {
httpResponse = new HttpResponse(this.httpConnection, this.charset, isAsync, this.isIgnoreResponseBody());
}
return httpResponse;
}
public class HttpConnection {
private final URL url;
private final Proxy proxy;
// 這個連接 HttpURLConnection ,是java提供的
private HttpURLConnection conn;
...// 省略無數代碼
}
3.2 一個例子
private String invoke(String url, String isMock, Map<String, Object> map) {
String result = HttpRequest.post(url).body(JSONUtil.toJsonStr(map)).execute().body();
return result;
}