本文是精講RestTemplate第3篇,前篇的blog訪問地址如下:
- 精講RestTemplate第1篇-在Spring或非Spring環境下如何使用
- 精講RestTemplate第2篇-多種底層HTTP客戶端類別庫的切換
RestTemplate可以發送HTTP GET請求,經常使用到的方法有兩個:
- getForObject()
- getForEntity()
二者的主要區別在于,getForObject()回傳值是HTTP協議的回應體,getForEntity()回傳的是ResponseEntity,ResponseEntity是對HTTP回應的封裝,除了包含回應體,還包含HTTP狀態碼、contentType、contentLength、Header等資訊,
為了方便后續開發測驗,首先介紹一個網站給大家,JSONPlaceholder是一個提供免費的在線REST API的網站,我們在開發時可以使用它提供的url地址測驗下網路請求以及請求引數,或者當我們程式需要獲取一些模擬資料、模擬圖片時也可以使用它,
一、 getForObject() 方法
1.1.以String的方式接受請求結果資料
在Spring Boot環境下寫一個單元測驗用例,以String型別接收回應結果資訊
@SpringBootTest
class ResttemplateWithSpringApplicationTests {
@Resource
private RestTemplate restTemplate;
@Test
void testSimple() {
String url = "http://jsonplaceholder.typicode.com/posts/1";
String str = restTemplate.getForObject(url, String.class);
System.out.println(str);
}
}
getForObject第二個引數為回傳值的型別,String.class以字串的形式接受getForObject回應結果,

1.2.以POJO物件的方式接受結果資料
在Spring Boot環境下寫一個單元測驗用例,以java POJO物件接收回應結果資訊
@Test
public void testPoJO() {
String url = "http://jsonplaceholder.typicode.com/posts/1";
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class);
System.out.println(postDTO.toString());
}
輸出列印結果如下:

POJO的定義如下,根據JSON String的資料格式定義,
@Data
public class PostDTO {
private int userId;
private int id;
private String title;
private String body;
}
1.3.以陣列的方式接收請求結果
訪問http://jsonplaceholder.typicode.com/posts 可以獲得JSON陣列方式的請求結果

下一步就是我們該如何接收,使用方法也很簡單,在Spring Boot環境下寫一個單元測驗用例,以陣列的方式接收請求結果,
@Test
public void testArrays() {
String url = "http://jsonplaceholder.typicode.com/posts";
PostDTO[] postDTOs = restTemplate.getForObject(url, PostDTO[].class);
System.out.println("陣列長度:" + postDTOs.length);
}
請求的結果被以陣列的方式正確接收,輸出如下:
陣列長度:100
1.4.使用占位符號傳參的幾種方式
以下的幾個請求都是在訪問"http://jsonplaceholder.typicode.com/posts/1",只是使用了占位符語法,這樣在業務使用上更加靈活,
- 使用占位符的形式傳遞引數:
String url = "http://jsonplaceholder.typicode.com/{1}/{2}";
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class, "posts", 1);
- 另一種使用占位符的形式:
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";
String type = "posts";
int id = 1;
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class, type, id);
- 我們也可以使用 map 裝載引數:
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";
Map<String,Object> map = new HashMap<>();
map.put("type", "posts");
map.put("id", 1);
PostDTO postDTO = restTemplate.getForObject(url, PostDTO.class, map);
二、getForEntity()方法
上面的所有的getForObject請求傳參方法,getForEntity都可以使用,使用方法上也幾乎是一致的,只是在回傳結果接收的時候略有差別,使用ResponseEntity<T> responseEntity來接收回應結果,用responseEntity.getBody()獲取回應體,回應體內容同getForObject方法回傳結果一致,剩下的這些回應資訊就是getForEntity比getForObject多出來的內容,
HttpStatus statusCode = responseEntity.getStatusCode();獲取整體的回應狀態資訊int statusCodeValue = https://www.cnblogs.com/zimug/p/responseEntity.getStatusCodeValue();獲取回應碼值HttpHeaders headers = responseEntity.getHeaders();獲取回應頭- 等
@Test
public void testEntityPoJo() {
String url = "http://jsonplaceholder.typicode.com/posts/5";
ResponseEntity<PostDTO> responseEntity
= restTemplate.getForEntity(url, PostDTO.class);
PostDTO postDTO = responseEntity.getBody(); // 獲取回應體
System.out.println("HTTP 回應body:" + postDTO.toString());
//以下是getForEntity比getForObject多出來的內容
HttpStatus statusCode = responseEntity.getStatusCode(); // 獲取回應碼
int statusCodeValue = https://www.cnblogs.com/zimug/p/responseEntity.getStatusCodeValue(); // 獲取回應碼值
HttpHeaders headers = responseEntity.getHeaders(); // 獲取回應頭
System.out.println("HTTP 回應狀態:" + statusCode);
System.out.println("HTTP 回應狀態碼:" + statusCodeValue);
System.out.println("HTTP Headers資訊:" + headers);
}
輸出列印結果

歡迎關注我的博客,里面有很多精品合集
- 本文轉載注明出處(必須帶連接,不能只轉文字):字母哥博客,
覺得對您有幫助的話,幫我點贊、分享!您的支持是我不竭的創作動力! ,另外,筆者最近一段時間輸出了如下的精品內容,期待您的關注,
- 《手摸手教你學Spring Boot2.0》
- 《Spring Security-JWT-OAuth2一本通》
- 《實戰前后端分離RBAC權限管理系統》
- 《實戰SpringCloud微服務從青銅到王者》
- 《VUE深入淺出系列》
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/107446.html
標籤:Java
