如何正確的使用Spring RestTemplate?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
成都創(chuàng)新互聯專注于企業(yè)成都全網營銷推廣、網站重做改版、靖邊網站定制設計、自適應品牌網站建設、H5高端網站建設、成都做商城網站、集團公司官網建設、外貿網站建設、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為靖邊等各大城市提供網站開發(fā)制作服務。
1.環(huán)境搭建
??為了演示RestTemplate的使用,我們創(chuàng)建兩個SpringBoot項目,一個provider作為server端,一個consumer作為服務調用方法
2.API方法介紹
API | 說明 |
---|---|
getForEntity() | 發(fā)送一個HTTP GET請求,返回的ResponseEntity包含了響應體所映射成的對象 |
getForObject() | 發(fā)送一個HTTP GET請求,返回的請求體將映射為一個對象 |
postForEntity() | POST 數據到一個URL,返回包含一個對象的ResponseEntity,這個對象是從響應體中映射得到的 |
postForObject() | POST 數據到一個URL,返回根據響應體匹配形成的對象 |
headForHeaders() | 發(fā)送HTTP HEAD請求,返回包含特定資源URL的HTTP頭 |
optionsForAllow() | 發(fā)送HTTP OPTIONS請求,返回對特定URL的Allow頭信息 |
postForLocation() | POST 數據到一個URL,返回新創(chuàng)建資源的URL |
put() | PUT 資源到特定的URL |
delete() | 在特定的URL上對資源執(zhí)行HTTP DELETE操作 |
exchange() | 在URL上執(zhí)行特定的HTTP方法,返回包含對象的ResponseEntity,這個對象是從響應體中映射得到的 |
execute() | 在URL上執(zhí)行特定的HTTP方法,返回一個從響應體映射得到的對象 |
3.具體使用
??我們通過常用的http協議的四種請求方式來看下效果
3.1 無參請求
??我們先來看下服務端請求方法不需要接收參數,
getForEntity
??通過getForEntity來實現
服務端
/** * 無參,返回字符串 * @return */ @GetMapping("/server1") public String server1String(){ System.out.println("服務端被訪問了..."); return "success"; }
調用
/** * RestTemplate 訪問 provider的第一個服務 server1 */ @Test public void contextLoads() { String url = "http://localhost:8080/server1"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class); // 獲取響應的狀態(tài) HttpStatus statusCode = entity.getStatusCode(); // 獲取響應的header信息 HttpHeaders headers = entity.getHeaders(); // 獲取響應的body信息 String msg = entity.getBody(); System.out.println(statusCode); System.out.println(headers); System.out.println(msg); }
輸出結果
說明:
1.getForEntity()方法執(zhí)行返回的類型是ResponseEntity<T>,ResponseEntity<T>是Spring對HTTP請求響應的封裝,包括了幾個重要的元素,如響應碼、contentType、contentLength、響應消息體等,在輸出結果中我們能夠看到
2.getForEntity()的參數中第一個是請求地址,第二個是T對應的類型
getForObject
??getForObject函數實際上是對getForEntity函數的進一步封裝,如果你只關注返回的消息體的內容,對其他信息都不關注,此時可以使用getForObject
/** * getForObject 訪問 */ @Test public void contextLoadsObject() { String url = "http://localhost:8080/server1"; RestTemplate restTemplate = new RestTemplate(); // 直接返回的就是我們需要的結果,但是獲取不到對應的響應狀態(tài)等信息 String msg = restTemplate.getForObject(url,String.class); System.out.println(msg); }
3.2 有參請求
服務端方法需要接收調用者傳遞的參數
/** * 有參,基本數據類型 返回字符串 * @return */ @RequestMapping("/server2") public String server2String(Integer id,String userName){ System.out.println("服務端被訪問了..."+id+" "+userName); return "success--參數得到了"; } /** * 有參,基本數據類型 返回字符串 * @return */ @RequestMapping("/server3") public String server3String(User user){ System.out.println("服務端被訪問了..."+user); return "success--參數得到了"; }
getForEntity
調用者可以通過兩種方式調用
第一種方式通過數字占位符,最后是一個可變長度的參數,來一一替換前面的占位符
/** * 請求服務并且傳遞參數 * 基本數據類型 */ @Test public void testServer2(){ // 參數在鏈接地址后 String url = "http://localhost:8080/server2?id={1}&userName={2}"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class,5,"bobo"); System.out.println(entity.getBody()); }
第二種就是使用name={name}這種形式,最后一個參數是一個map,map的key即為前邊占位符的名字,map的value為參數值
/** * 請求服務并且傳遞參數 * 基本數據類型 */ @Test public void testServer3(){ String url = "http://localhost:8080/server2?id={id}&userName={userName}"; Map<String,Object> map = new HashMap<>(); map.put("id",6); map.put("userName","波波烤鴨"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class,map); System.out.println(entity.getBody()); }
postForEntity
??如果是post方式提交請求傳遞參數我們可以這樣使用,如下
服務端:注意要加@RequestBody注解
/** * 有參,基本數據類型 返回字符串 * @return */ @RequestMapping("/server3") public String server3String(@RequestBody User user){ System.out.println("服務端被訪問了..."+user); return "success--參數得到了"; }
客戶端
/** * postForEntity(url,user,String.class) * url:請求地址 * user:請求提交的數據 * String.class 接收返回數據的類型 */ @Test public void contextLoadsObject1() { String url = "http://localhost:8080/server3"; RestTemplate restTemplate = new RestTemplate(); User user = new User(1,"bobo","中國"); // 直接返回的就是我們需要的結果,但是獲取不到對應的響應狀態(tài)等信息 String msg = restTemplate.postForEntity(url,user,String.class).getBody(); System.out.println(msg); }
3.3 返回自己類型
服務端返回的我們自定義類型的數據
/** * 返回自定義對象 * @return */ @RequestMapping("/server4") public User server4Object(){ System.out.println("服務端被訪問了..."); return new User(2,"李四","深圳"); }
客戶端:
/** * 返回類型為自定義類型 */ @Test public void testServer5(){ String url = "http://localhost:8080/server4"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity<User> entity = restTemplate.getForEntity(url, User.class); System.out.println(entity.getBody()); }
使用getForEntity和getForObject及postForEntity和postForObject都差不多,注意接收的類型即可。
3.4 返回的list帶泛型的場景
此處我們需要使用到exchange方法,特定如下
允許調用者指定HTTP請求的方法(GET,POST,PUT等)
可以在請求中增加body以及頭信息,其內容通過參‘HttpEntity<?>requestEntity'描述
exchange支持‘含參數的類型'(即泛型類)作為返回類型,該特性通過‘ParameterizedTypeReferenceresponseType'描述
客戶端調用
/** * 返回 集合帶泛型 * @return */ @RequestMapping("/server5") public List<User> server5List(){ System.out.println("服務端被訪問了..."); return Arrays.asList(new User(2,"李四1","深圳") ,new User(3,"李四2","深圳") ,new User(4,"李四3","深圳")); }
關于如何正確的使用Spring RestTemplate問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯行業(yè)資訊頻道了解更多相關知識。
分享標題:如何正確的使用SpringRestTemplate
分享鏈接:http://chinadenli.net/article6/gsggig.html
成都網站建設公司_創(chuàng)新互聯,為您提供ChatGPT、App開發(fā)、手機網站建設、虛擬主機、搜索引擎優(yōu)化、品牌網站設計
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯