強(qiáng)大又優(yōu)雅!Spring Boot 中 RestTemplate 的最佳實(shí)踐詳解
在現(xiàn)代開發(fā)中,API 的設(shè)計(jì)和調(diào)用變得尤為重要,尤其是基于 REST 架構(gòu)的服務(wù)調(diào)用。RestTemplate 是 Spring 提供的用于同步調(diào)用 RESTful 服務(wù)的強(qiáng)大工具,它支持各種 HTTP 方法,如 GET、POST、PUT、DELETE 等。作為開發(fā)者,理解并掌握如何高效使用 RestTemplate 是優(yōu)化服務(wù)交互性能的重要一步。本文旨在深入探討 RestTemplate 的 POST 請求方法以及 exchange() 和 execute() 等常用方法,幫助你在實(shí)際開發(fā)中靈活使用這些工具應(yīng)對復(fù)雜的業(yè)務(wù)場景。
通過示例代碼,我們將詳細(xì)演示如何使用 RestTemplate 進(jìn)行 POST 請求,包括如何設(shè)置請求頭和請求體、如何構(gòu)建和傳遞復(fù)雜數(shù)據(jù),以及如何處理返回的響應(yīng)。同時(shí),我們還將探索如何使用 exchange() 指定 HTTP 方法,實(shí)現(xiàn)靈活的請求處理。無論是初學(xué)者還是有經(jīng)驗(yàn)的開發(fā)者,這篇文章都將為你提供有價(jià)值的參考。
RestTemplate 是 Spring 提供的用于訪問 REST 服務(wù)的客戶端。
它提供了多種便捷的方法來訪問遠(yuǎn)程 HTTP 服務(wù),大大提高了客戶端代碼開發(fā)的效率。
之前,我使用 Apache 的 HttpClient 開發(fā) HTTP 請求。代碼非常復(fù)雜,我不得不管理資源清理等問題,代碼繁瑣且包含大量冗余部分。
以下是我封裝的一個(gè) post 請求工具的截圖:
圖片
本教程將指導(dǎo)你如何在 Spring 生態(tài)系統(tǒng)中使用 RestTemplate 進(jìn)行 GET 和 POST 請求,并通過 exchange 方法來指定請求類型。同時(shí),還會(huì)分析 RestTemplate 的核心方法。
閱讀完本教程后,你將能夠以優(yōu)雅的方式進(jìn)行 HTTP 請求。
RestTemplate 簡介
*RestTemplate* 是 *Spring* 中用于同步客戶端通信的核心類。它簡化了與 HTTP 服務(wù)的通信,并遵循 RestFul 原則。代碼只需提供一個(gè) URL 并提取結(jié)果。
默認(rèn)情況下,RestTemplate 依賴于 JDK 的 HTTP 連接工具。但是,你可以通過 setRequestFactory 屬性切換到其他 HTTP 工具源,例如 Apache HttpComponents、Netty 和 OkHttp。
RestTemplate 大大簡化了表單數(shù)據(jù)的提交,并包含對 JSON 數(shù)據(jù)的自動(dòng)轉(zhuǎn)換。
然而,要真正掌握它的使用,必須理解 HttpEntity 的結(jié)構(gòu)(包括 headers 和 body)以及它與 uriVariables 之間的區(qū)別。
這一點(diǎn)在 POST 請求中尤為明顯,稍后我們將詳細(xì)討論。
此類的主要入口點(diǎn)基于六種 HTTP 方法:
圖片
此外,exchange 和 execute 可以與上述方法互換使用。
在內(nèi)部,RestTemplate 默認(rèn)使用 HttpMessageConverter 實(shí)例,將 HTTP 消息轉(zhuǎn)換為 POJO或?qū)?nbsp;POJO 轉(zhuǎn)換為 HTTP 消息。默認(rèn)情況下,它會(huì)為主要的 MIME 類型注冊轉(zhuǎn)換器,但你也可以通過 setMessageConverters 注冊其他轉(zhuǎn)換器。
(在實(shí)際使用中,這一點(diǎn)并不十分明顯;許多方法都有一個(gè) responseType 參數(shù),你可以傳遞一個(gè)與響應(yīng)體映射的對象,底層的 HttpMessageConverter 會(huì)進(jìn)行映射。)
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
HttpMessageConverter.java 源代碼:
public interface HttpMessageConverter<T> {
// 判斷該轉(zhuǎn)換器是否可以讀取給定的類。
boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
// 判斷該轉(zhuǎn)換器是否可以寫入給定的類。
boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);
// 返回一個(gè) List<MediaType>
List<MediaType> getSupportedMediaTypes();
// 讀取輸入消息
T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException;
// 將對象寫入輸出消息
void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException;
}
在內(nèi)部,RestTemplate 默認(rèn)使用 SimpleClientHttpRequestFactory 和 DefaultResponseErrorHandler 來分別處理 HTTP 請求的創(chuàng)建和錯(cuò)誤處理。
然而,你可以通過 setRequestFactory 和 setErrorHandler 來覆蓋這些默認(rèn)行為。
GET 請求
getForObject() 方法
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables){}
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForObject(URI url, Class<T> responseType)
getForObject() 方法可以將 HTTP 響應(yīng)直接轉(zhuǎn)換為 POJO,而不像 getForEntity() 需要額外處理響應(yīng)。
getForObject 會(huì)直接返回 POJO,省略了大量響應(yīng)信息。
POJO 示例:
public class Notice {
private int status;
private Object msg;
private List<DataBean> data;
}
public class DataBean {
private int noticeId;
private String noticeTitle;
private Object noticeImg;
private long noticeCreateTime;
private long noticeUpdateTime;
private String noticeContent;
}
無參數(shù)的 GET 請求
/**
* 無參數(shù)的 GET 請求
*/
@Test
public void restTemplateGetTest(){
RestTemplate restTemplate = new RestTemplate();
Notice notice = restTemplate.getForObject("http://icoderoad.com/notice/list/1/5", Notice.class);
System.out.println(notice);
}
控制臺(tái)輸出:
INFO 19076 --- [ main] c.w.s.c.w.c.HelloControllerTest
: Started HelloControllerTest in 5.532 seconds (JVM running for 7.233)
Notice{status=200, msg=null, data=[DataBean{noticeId=21, noticeTitle='aaa', noticeImg=null,
noticeCreateTime=1525292723000, noticeUpdateTime=1525292723000, noticeContent='<p>aaa</p>'},
DataBean{noticeId=20, noticeTitle='ahaha', noticeImg=null, noticeCreateTime=1525291492000,
noticeUpdateTime=1525291492000, noticeContent='<p>ah.......'
帶參數(shù)的 GET 請求 1
Notice notice = restTemplate.getForObject("http://icoderoad.com/notice/list/{1}/{2}", Notice.class, 1, 5);
可以看到,使用了占位符 {1} 和 {2}。
帶參數(shù)的 GET 請求 2
Map<String, String> map = new HashMap<>();
map.put("start", "1");
map.put("page", "5");
Notice notice = restTemplate.getForObject("http://icoderoad.com/notice/list/", Notice.class, map);
在這種情況下,使用 Map 加載參數(shù),默認(rèn)情況下會(huì)以 PathVariable 格式解析 URL。
getForEntity() 方法
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables){}
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables){}
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType){}
與 getForObject() 不同,此方法返回 ResponseEntity 對象。
如果需要將其轉(zhuǎn)換為 POJO,則必須使用 JSON 工具類,可根據(jù)個(gè)人偏好選擇。
對于不熟悉解析 JSON 的人,可以查閱工具如 FastJson 或 Jackson。接下來我們來探討 ResponseEntity 的相關(guān)方法。
ResponseEntity、HttpStatus 和 BodyBuilder 的結(jié)構(gòu)
//ResponseEntity.java
public HttpStatus getStatusCode(){}
public int getStatusCodeValue(){}
public boolean equals(@Nullable Object other) {}
public String toString() {}
public static BodyBuilder status(HttpStatus status) {}
public static BodyBuilder ok() {}
public static <T> ResponseEntity<T> ok(T body) {}
public static BodyBuilder created(URI location) {}
...
//HttpStatus.java
public enum HttpStatus {
public boolean is1xxInformational() {}
public boolean is2xxSuccessful() {}
public boolean is3xxRedirection() {}
public boolean is4xxClientError() {}
public boolean is5xxServerError() {}
public boolean isError() {}
}
//BodyBuilder.java
public interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
// 通過 Content-Length 頭設(shè)置響應(yīng)實(shí)體的內(nèi)容長度
BodyBuilder contentLength(long contentLength);
// 設(shè)置響應(yīng)實(shí)體的 MediaType
BodyBuilder contentType(MediaType contentType);
// 設(shè)置響應(yīng)實(shí)體的內(nèi)容并返回
<T> ResponseEntity<T> body(@Nullable T body);
}
如上所示,ResponseEntity 包含了來自 HttpStatus 和 BodyBuilder 的信息,這使得處理原始響應(yīng)變得更加簡單。
示例:
@Test
public void rtGetEntity(){
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Notice> entity = restTemplate.getForEntity("http://icoderoad.com/notice/list/1/5", Notice.class);
HttpStatus statusCode = entity.getStatusCode();
System.out.println("statusCode.is2xxSuccessful()" + statusCode.is2xxSuccessful());
Notice body = entity.getBody();
System.out.println("entity.getBody()" + body);
ResponseEntity.BodyBuilder status = ResponseEntity.status(statusCode);
status.contentLength(100);
status.body("在此處添加一個(gè)聲明");
ResponseEntity<Class<Notice>> body1 = status.body(Notice.class);
Class<Notice> body2 = body1.getBody();
System.out.println("body1.toString()" + body1.toString());
}
輸出:
statusCode.is2xxSuccessful() true
entity.getBody() Notice{status=200, msg=null, data=[DataBean{noticeId=21, noticeTitle='aaa', ...
body1.toString() <200 OK, class com.waylau.spring.cloud.weather.pojo.Notice, {Content-Length=[100]}>
當(dāng)然,像 getHeaders() 這樣的方法也是可用的,但在此不作示例。
POST 請求
類似于 GET 請求,POST 請求也有 postForObject 和 postForEntity 方法。
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException {}
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException {}
public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException {}
示例
這里我使用一個(gè)郵箱驗(yàn)證接口進(jìn)行測試。
@Test
public void rtPostObject(){
RestTemplate restTemplate = new RestTemplate();
String url = "http://47.xxx.xxx.96/register/checkEmail";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("email", "844072586@qq.com");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
System.out.println(response.getBody());
}
執(zhí)行結(jié)果:
{"status":500,"msg":"該郵箱已經(jīng)注冊","data":null}
為什么使用 MultiValueMap?
MultiValueMap 是 Map 的子類,它允許每個(gè)鍵存儲(chǔ)多個(gè)值。這里簡單介紹該接口:
public interface MultiValueMap<K, V> extends Map<K, List<V>> {...}
使用 MultiValueMap 的原因是 HttpEntity 接受的請求類型為 MultiValueMap:
public HttpEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers){}
在這個(gè)構(gòu)造函數(shù)中,我們傳入的 map 是請求體,headers 是請求頭。
我們使用 HttpEntity 是因?yàn)?,盡管 restTemplate.postForEntity 方法似乎接受 @Nullable Object request 類型,但如果深入追溯,會(huì)發(fā)現(xiàn)這個(gè) request 是通過 HttpEntity 解析的。核心代碼如下:
if (requestBody instanceof HttpEntity) {
this.requestEntity = (HttpEntity<?>) requestBody;
} else if (requestBody != null) {
this.requestEntity = new HttpEntity<>(requestBody);
} else {
this.requestEntity = HttpEntity.EMPTY;
}
我曾嘗試使用 map 傳遞參數(shù),雖然編譯時(shí)沒有報(bào)錯(cuò),但請求無效,最終出現(xiàn) 400 錯(cuò)誤。
這種請求方式已經(jīng)滿足 POST 請求的需求,同時(shí),cookie 作為請求頭的一部分,也可以根據(jù)需要進(jìn)行設(shè)置。
其他方法與此類似。
使用 exchange 指定 HTTP 方法
exchange() 方法與 getForObject()、getForEntity()、postForObject() 和 postForEntity() 不同之處在于它允許你指定 HTTP 方法。
你會(huì)注意到,所有的 exchange 方法似乎都有 @Nullable HttpEntity<?> requestEntity 參數(shù),這意味著我們需要使用 HttpEntity 來傳遞請求體。正如前面提到的,使用 HttpEntity性能更好。
示例
@Test
public void rtExchangeTest() throws JSONException {
RestTemplate restTemplate = new RestTemplate();
String url = "http://icoderoad.com/notice/list";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
JSONObject jsonObj = new JSONObject();
jsonObj.put("start", 1);
jsonObj.put("page", 5);
HttpEntity<String> entity = new HttpEntity<>(jsonObj.toString(), headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, JSONObject.class);
System.out.println(exchange.getBody());
}
這次我使用了 JSONObject 來傳遞和返回?cái)?shù)據(jù)。其他 HttpMethod 方法的使用類似。
使用 execute 指定 HTTP 方法
execute() 方法類似于 exchange(),它允許你指定不同的 HttpMethod 類型。但不同之處在于它返回的響應(yīng)體是一個(gè)對象 <T>,而不是 ResponseEntity<T>。
需要強(qiáng)調(diào)的是,execute() 方法是上述所有方法的底層實(shí)現(xiàn)。以下是一個(gè)示例:
@Override
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}
結(jié)語
通過對 RestTemplate 不同方法的深入講解,特別是 POST 請求的使用以及 exchange()、execute() 的靈活運(yùn)用,本文展示了在開發(fā)過程中如何使用這些方法簡化與外部服務(wù)的交互,并提升代碼的可讀性和維護(hù)性。在面對復(fù)雜的業(yè)務(wù)需求時(shí),掌握這些技術(shù)將幫助開發(fā)者在請求數(shù)據(jù)、處理響應(yīng)以及提升 API 性能方面取得更好的平衡。
對于高效的 HTTP 請求處理,RestTemplate 提供了豐富的功能,靈活支持多種請求方式和參數(shù)配置,極大地簡化了開發(fā)流程。隨著項(xiàng)目復(fù)雜度的增加,理解和掌握這些工具的使用技巧,能夠大大提升開發(fā)效率,同時(shí)減少潛在的錯(cuò)誤。通過深入研究 RestTemplate,我們可以構(gòu)建出更加健壯、高效的服務(wù)交互機(jī)制,滿足不斷變化的業(yè)務(wù)需求。希望本文能為你提供深入的理解,助力你的開發(fā)之旅。