Class StandardHttpClient
java.lang.Object
io.leandev.appfuse.http.StandardHttpClient
- All Implemented Interfaces:
AutoCloseable
標準 HTTP 客戶端實作
基於 Apache HttpClient 5.x 的高階 HTTP 客戶端封裝,提供:
- 自動重試機制(線性退避策略)
- Gateway 轉發支援
- 認證令牌自動添加
- 攔截器機制(before/after/error)
- 連線池統計
- 自動異常映射(HTTP 狀態碼 → 異常類別)
使用範例
```java // 基本使用 try (StandardHttpClient client = new StandardHttpClient()) { ClassicHttpRequest request = new HttpGet("https://api.example.com/users"); ClassicHttpResponse response = client.execute(request); // 處理回應 }// 使用 Builder 配置 StandardHttpClient client = StandardHttpClient.builder() .withMaxTotal(200) .withTimeout(30) .withLogging() .withAuthenticator(() -> getAccessToken()) .build();
// 配合 HttpRequestParser/HttpResponseParser ClassicHttpResponse response = client.execute(request); User user = client.getResponseParser().read(response, User.class);
<h2>重試機制</h2>
支援自動重試失敗的 HTTP 請求,使用線性退避策略:
- 預設重試次數:0(不重試)
- 可透過 `setRetries(int)` 設定重試次數
- 使用 [LinearBackoffRetryStrategy] 進行重試
<h2>Gateway 轉發</h2>
支援將所有請求透過指定的 Gateway 轉發:
```java
client.setGateway(new URL("http://localhost:8080"));
// 所有請求都會透過 http://localhost:8080 轉發
攔截器機制
提供三種攔截時機:beforeRequest- 請求發送前afterResponse- 回應接收後onError- 發生錯誤時
異常處理
自動將 HTTP 錯誤狀態碼映射為對應的異常:- 400 →
BadRequestException - 401 →
NotAuthorizedException - 403 →
ForbiddenException - 404 →
NotFoundException - 500 →
InternalServerErrorException - 503 →
ServiceUnavailableException
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic class -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionStandardHttpClient(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager connectionManager) StandardHttpClient(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager connectionManager, org.apache.hc.client5.http.config.RequestConfig requestConfig) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddInterceptor(HttpClientInterceptor interceptor) 新增攔截器builder()voidclose()org.apache.hc.core5.http.ClassicHttpResponseexecute(org.apache.hc.core5.http.ClassicHttpRequest request) org.apache.hc.core5.http.ClassicHttpResponseexecute(org.apache.hc.core5.http.ClassicHttpRequest request, int retries) protected voidhandleHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse response, org.apache.hc.core5.http.ClassicHttpRequest request) 處理 HTTP 回應,檢查狀態碼並拋出對應的異常voidsetGateway(URL gateway) 設定 Gateway URL
-
Field Details
-
FORWARDED_URL_HEADER
-
-
Constructor Details
-
StandardHttpClient
public StandardHttpClient() -
StandardHttpClient
public StandardHttpClient(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager connectionManager) -
StandardHttpClient
public StandardHttpClient(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager connectionManager, org.apache.hc.client5.http.config.RequestConfig requestConfig)
-
-
Method Details
-
setGateway
設定 Gateway URL
設定後,所有請求都會透過 Gateway 轉發。
- Parameters:
gateway- Gateway URL
-
addInterceptor
-
execute
public org.apache.hc.core5.http.ClassicHttpResponse execute(org.apache.hc.core5.http.ClassicHttpRequest request) -
execute
public org.apache.hc.core5.http.ClassicHttpResponse execute(org.apache.hc.core5.http.ClassicHttpRequest request, int retries) -
handleHttpResponse
protected void handleHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse response, org.apache.hc.core5.http.ClassicHttpRequest request) 處理 HTTP 回應,檢查狀態碼並拋出對應的異常- Parameters:
response- HTTP 回應request- 原始請求(用於錯誤訊息)- Throws:
RuntimeException- 如果狀態碼 >= 400
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
getTotalStats
-
builder
-