一、Spring Boot調用外部介面概述
在開發過程中,常常需要將本地項目和外部服務進行交互,這時就需要使用到 Spring Boot 調用外部介面。那麼 Spring Boot 是如何調用外部介面的呢?需要哪些技術和方法呢?
要使用 Spring Boot 調用外部介面,需要用到 HTTP 客戶端,比較常用的有 Apache HttpClient 和 HttpComponents,本文以 Apache HttpClient 為例進行闡述。
二、添加Apache HttpClient依賴
首先,在 pom.xml 中添加 Apache HttpClient 的依賴:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
三、使用 Apache HttpClient 調用外部介面
1、GET請求
使用 Apache HttpClient 發送 GET 請求非常簡單,只需要創建一個 HttpClient 對象和一個 HttpGet 對象,然後通過 execute 方法執行即可:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.example.com");
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
// do something with the response
}
} finally {
response.close();
}
在這個例子中,我們使用 HttpClients.createDefault 方法創建一個 HttpClient 對象,用 HttpGet 構造方法創建一個 HttpGet 對象,然後通過 execute 方法執行 HTTP GET 請求,並將響應結果保存在 CloseableHttpResponse 對象中。最後,我們需要在finally塊中關閉響應對象。
2、POST請求
通過 Apache HttpClient 發送 POST 請求也非常簡單,步驟和發送 GET 請求基本一致。只是需要創建一個 HttpPost 對象,將請求參數封裝在一個 HttpEntity 對象中,然後通過 setEntity 方法設置給 HttpPost 對象即可:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.example.com");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "admin"));
nvps.add(new BasicNameValuePair("password", "123456"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
// do something with the response
}
} finally {
response.close();
}
在這個例子中,我們創建了一個 HttpPost 對象,通過 setEntity 方法將請求參數封裝在一個 UrlEncodedFormEntity 對象中,並將其設置給 HttpPost 對象。然後,通過 execute 方法執行 HTTP POST 請求,並將響應結果保存在 CloseableHttpResponse 對象中,最後在finally塊中關閉響應對象。
四、使用 RestTemplate 調用外部介面
Spring 提供了一個 RestTemplate 類,它採用了模板方法(Template Method)的形式,讓調用外部介面變得更加簡單。使用 RestTemplate 可以完全省略 Apache HttpClient 的使用過程,代碼更加簡潔易讀。
1、添加 RestTemplate 依賴
與 Apache HttpClient 類似,我們需要在 pom.xml 中添加 Spring Web 的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、使用 RestTemplate 發送 GET 請求
使用 RestTemplate 發送 GET 請求非常簡單,只需要創建一個 RestTemplate 對象,然後調用它的 getForObject 方法即可:
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://www.example.com", String.class);
在這個例子中,我們創建了一個 RestTemplate 對象,並通過 getForObject 方法發送 GET 請求。getForObject 方法有兩個參數,第一個參數是請求的 URL,第二個參數是 HTTP 響應的數據類型。這裡我們將 HTTP 響應的數據類型設置為 String,所以 getForObject 方法返回的是一個 String 對象。
3、使用 RestTemplate 發送 POST 請求
使用 RestTemplate 發送 POST 請求也非常簡單,只需要創建一個 RestTemplate 對象,然後調用它的 postForObject 方法即可:
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("username", "admin");
map.add("password", "123456");
String result = restTemplate.postForObject("http://www.example.com", map, String.class);
在這個例子中,我們創建了一個 RestTemplate 對象,並通過 postForObject 方法發送 POST 請求。postForObject 方法有三個參數,第一個參數是請求的 URL,第二個參數是請求參數,第三個參數是 HTTP 響應的數據類型。這裡我們將 HTTP 響應的數據類型設置為 String,所以 postForObject 方法返回的是一個 String 對象。
五、總結
Spring Boot 調用外部介面只需要使用 Apache HttpClient 或 RestTemplate 對象,同時添加相應的依賴即可。使用 RestTemplate 可以更加簡潔地發送 HTTP 請求,減少開發工作量。需要注意的是,調用外部介面時需要處理異常和錯誤情況,保證系統的健壯性和穩定性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/152885.html