HttpComponentsClientHttpRequestFactory 現在需要 5 Apache HttpClient

在 Spring Framework 6 中,支援 Apache HttpClient 4.x 已從 HttpComponentsClientHttpRequestFactory.依賴此工廠的元件,例如 RestTemplate 現在必須使用 Apache HttpClient 5.x,它使用 org.apache.hc.* 套件中使用了 Apache XMLBeans 類別。

任何使用 HttpClient 4.x 類別,例如 CloseableHttpClient, HttpClient, RequestConfig,或 HttpContextorg.apache.http.* Spring 的 HTTP 用戶端基礎架構中的包(包括涉及到的構建器、方法調用或配置) HttpComponentsClientHttpRequestFactory) 會導致編譯錯誤,必須更新為使用 HttpClient 5.x 等同物。

檢視下列方法和構成程式 HttpComponentsClientHttpRequestFactory 以確保它們不會引用 HttpClient 4.x API:

以下範例顯示會被標記的無效用法:


import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class InvalidHttp4ClientUsage {
    public void example() {
      CloseableHttpClient client = HttpClients.custom().build();
      RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
        }
    }

以下範例顯示有效的移轉用法:


import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class ValidHttpClientUsage {
    public void example() {
      CloseableHttpClient client = HttpClients.custom().build();
      RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
        }
    }

如需相關資訊,請參閱下列資源: