精講RestTemplate第4篇-POST請求方法使用詳解

本文是精講RestTemplate第4篇,前篇的blog訪問地址以下:html

若是您閱讀完本文章,以爲對您有幫助,請幫忙點個贊,您的支持是我不竭的創做動力vue

在上一節爲你們介紹了RestTemplate的GET請求的兩個方法:getForObject()和getForEntity()。其實POST請求方法和GET請求方法上大同小異,RestTemplate的POST請求也包含兩個主要方法:java

  • postForObject()
  • postForEntity()

兩者的主要區別在於,postForObject()返回值是HTTP協議的響應體。postForEntity()返回的是ResponseEntity,ResponseEntity是對HTTP響應的封裝,除了包含響應體,還包含HTTP狀態碼、contentType、contentLength、Header等信息。spring

1、postForObject發送JSON格式請求

寫一個單元測試用例,測試用例的內容是向指定的URL提交一個Post(帖子).json

@SpringBootTest
class PostTests {

   @Resource
   private RestTemplate restTemplate;

   @Test
   void testSimple()  {
      // 請求地址
      String url = "http://jsonplaceholder.typicode.com/posts";

      // 要發送的數據對象
      PostDTO postDTO = new PostDTO();
      postDTO.setUserId(110);
      postDTO.setTitle("zimug 發佈文章");
      postDTO.setBody("zimug 發佈文章 測試內容");

      // 發送post請求,並輸出結果
      PostDTO result = restTemplate.postForObject(url, postDTO, PostDTO.class);
      System.out.println(result);
   }
}
  • jsonplaceholder.typicode.com是一個能夠提供在線免費RESTful測試服務的一個網站
  • 」/posts"服務接收PostDTO 參數對象,並將請求結果以JSON字符串的形式進行響應。響應結果就是請求參數對象對應的JSON字符串。
  • 因此postForObject方法第二個參數是請求數據對象,第三個參數是返回值類型

最終將返回值的打印結果以下:
後端

2、postForObject模擬表單數據提交

下面給你們寫一個使用postForObject模擬表單數據提交的例子,即:提交x-www-form-urlencoded格式的數據springboot

@Test
public void testForm() {
   // 請求地址
   String url = "http://jsonplaceholder.typicode.com/posts";

   // 請求頭設置,x-www-form-urlencoded格式的數據
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

   //提交參數設置
   MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
   map.add("title", "zimug 發佈文章第二篇");
   map.add("body", "zimug 發佈文章第二篇 測試內容");

   // 組裝請求體
   HttpEntity<MultiValueMap<String, String>> request =
               new HttpEntity<MultiValueMap<String, String>>(map, headers);

   // 發送post請求,並打印結果,以String類型接收響應結果JSON字符串
   String result = restTemplate.postForObject(url, request, String.class);
   System.out.println(result);
}

請求數據打印結果以下:
前後端分離

3、 url支持佔位符語法

若是url地址上面須要傳遞一些動態參數,可使用佔位符的方式:微服務

String url = "http://jsonplaceholder.typicode.com/{1}/{2}";
String url = "http://jsonplaceholder.typicode.com/{type}/{id}";

具體的用法和使用GET方法請求是一致的,因此請參考: 精講RestTemplate第3篇-GET請求使用方法詳解post

4、postForEntity()方法

上面的全部的postForObject請求傳參方法,postForEntity均可以使用,使用方法上也幾乎是一致的,只是在返回結果接收的時候略有差異。使用ResponseEntity<T> responseEntity來接收響應結果。用responseEntity.getBody()獲取響應體。響應體內容同postForObject方法返回結果一致。剩下的這些響應信息就是postForEntity比postForObject多出來的內容。

  • HttpStatus statusCode = responseEntity.getStatusCode(); 獲取總體的響應狀態信息
  • int statusCodeValue = responseEntity.getStatusCodeValue(); 獲取響應碼值
  • HttpHeaders headers = responseEntity.getHeaders(); 獲取響應頭
@Test
public void testEntityPoJo() {
   // 請求地址
   String url = "http://jsonplaceholder.typicode.com/posts";

   // 要發送的數據對象
   PostDTO postDTO = new PostDTO();
   postDTO.setUserId(110);
   postDTO.setTitle("zimug 發佈文章");
   postDTO.setBody("zimug 發佈文章 測試內容");

   // 發送post請求,並輸出結果
   ResponseEntity<String> responseEntity
               = restTemplate.postForEntity(url, postDTO, String.class);
   String body = responseEntity.getBody(); // 獲取響應體
   System.out.println("HTTP 響應body:" + postDTO.toString());


   //如下是postForEntity比postForObject多出來的內容
   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);
}

輸出打印結果

5、postForLocation() 方法的使用

postForLocation的傳參的類型、個數、用法基本都和postForObject()或postForEntity()一致。和前二者的惟一區別在於返回值是一個URI。該URI返回值體現的是:用於提交完成數據以後的頁面跳轉,或數據提交完成以後的下一步數據操做URI。

@Test
public void testURI() {
   // 請求地址
   String url = "http://jsonplaceholder.typicode.com/posts";

   PostDTO postDTO = new PostDTO();
   postDTO.setUserId(110);
   postDTO.setTitle("zimug 發佈文章");
   postDTO.setBody("zimug 發佈文章 測試內容");

   // 發送post請求,並輸出結果
   URI uri = restTemplate.postForLocation(url,postDTO);
   System.out.println(uri);
}

輸出結果以下,含義是:提交了post以後,該post的id是101,能夠經過以下的鏈接去獲取數據。

歡迎關注個人博客,裏面有不少精品合集

  • 本文轉載註明出處(必須帶鏈接,不能只轉文字):字母哥博客

以爲對您有幫助的話,幫我點贊、分享!您的支持是我不竭的創做動力! 。另外,筆者最近一段時間輸出了以下的精品內容,期待您的關注。

相關文章
相關標籤/搜索