本文是精講RestTemplate第3篇,前篇的blog訪問地址如下:
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 = 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 = responseEntity.getStatusCodeValue(); // 獲取響應碼值
HttpHeaders headers = responseEntity.getHeaders(); // 獲取響應頭
System.out.println("HTTP 響應狀態:" + statusCode);
System.out.println("HTTP 響應狀態碼:" + statusCodeValue);
System.out.println("HTTP Headers信息:" + headers);
}
輸出打印結果
歡迎關注我的博客,里面有很多精品合集
- 本文轉載注明出處(必須帶連接,不能只轉文字):字母哥博客。
覺得對您有幫助的話,幫我點贊、分享!您的支持是我不竭的創作動力! 。另外,筆者最近一段時間輸出了如下的精品內容,期待您的關注。