天天看點

Java調用外部REST請求的幾種方式

1 restTemplate — spring 提供

特點:

1、RestOperations 提供了各種封裝方法,非常友善直接将傳回轉成實體類。

2、預設使用JDK 的HttpURLConnection進行通信,但是可以通過RestTemplate.setRequestFactory 切換到不同的HTTP源:如Apache HttpComponents、Netty、OkHttp。

3、支援同步、異步請求;

4、支援更多的定制,比如攔截器等。

ps:支援 get 請求,參數是 body 的形式。

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 傳參。​

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;
}
      

所有發生在我們身上的事件都是一個經過仔細包裝的禮物。隻要我們願意面對它有時候有點醜惡的包裝,帶着耐心和勇氣一點一點的拆開包裝的話,我們會驚喜的看到裡面珍藏的禮物。

----遇見未知的自己