本文是精講RestTemplate第4篇,前篇的blog訪問地址以下:html
若是您閱讀完本文章,以爲對您有幫助,請幫忙點個贊,您的支持是我不竭的創做動力vue
在上一節爲你們介紹了RestTemplate的GET請求的兩個方法:getForObject()和getForEntity()。其實POST請求方法和GET請求方法上大同小異,RestTemplate的POST請求也包含兩個主要方法:java
兩者的主要區別在於,postForObject()返回值是HTTP協議的響應體。postForEntity()返回的是ResponseEntity,ResponseEntity是對HTTP響應的封裝,除了包含響應體,還包含HTTP狀態碼、contentType、contentLength、Header等信息。spring
寫一個單元測試用例,測試用例的內容是向指定的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); } }
最終將返回值的打印結果以下:後端
下面給你們寫一個使用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); }
請求數據打印結果以下:前後端分離
若是url地址上面須要傳遞一些動態參數,可使用佔位符的方式:微服務
String url = "http://jsonplaceholder.typicode.com/{1}/{2}"; String url = "http://jsonplaceholder.typicode.com/{type}/{id}";
具體的用法和使用GET方法請求是一致的,因此請參考: 精講RestTemplate第3篇-GET請求使用方法詳解post
上面的全部的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); }
輸出打印結果
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,能夠經過以下的鏈接去獲取數據。
以爲對您有幫助的話,幫我點贊、分享!您的支持是我不竭的創做動力! 。另外,筆者最近一段時間輸出了以下的精品內容,期待您的關注。