SpringBoot中RestTemplate的使用詳解
RestTemplate的使用
GET請求
在RestTemplate中,發(fā)送一個GET請求,我們可以通過如下兩種方式:
第一種:
getForEntity getForEntity方法的返回值是一個 ResponseEntity , ResponseEntity 是 Spring 對 HTTP 請求響應的封裝,包括了幾個重要的元素,如響應碼、contentType、contentLength、響應消息體等。比如下面一個例子:
@RequestMapping("/gethello") public String getHello() { ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class); String body = responseEntity.getBody(); HttpStatus statusCode = responseEntity.getStatusCode(); int statusCodeValue = responseEntity.getStatusCodeValue(); HttpHeaders headers = responseEntity.getHeaders(); StringBuffer result = new StringBuffer(); result.append("responseEntity.getBody():").append(body).append("<hr>") .append("responseEntity.getStatusCode():").append(statusCode).append("<hr>") .append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("<hr>") .append("responseEntity.getHeaders():").append(headers).append("<hr>"); return result.toString(); }
getForEntity 的第一個參數(shù)為要調用的服務的地址,這里調用了服務提供者提供的/hello接口,注意這里是通過服務名調用而不是服務地址,如果寫成服務地址就沒法實現(xiàn)客戶端負載均衡了。
getForEntity 第二個參數(shù)String.class表示希望返回的body類型是String類型拿到返回結果之后,將返回結果遍歷打印出來 最終顯示結果如下:
有時候我在調用服務提供者提供的接口時,可能需要傳遞參數(shù),有兩種不同的方式,如下:
@RequestMapping("/sayhello") public String sayHello() { ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={1}", String.class, "張三"); return responseEntity.getBody(); }
@RequestMapping("/sayhello2") public String sayHello2() { Map<String, String> map = new HashMap<>(); map.put("name", "李四"); ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={name}", String.class, map); return responseEntity.getBody(); }
- 可以用一個數(shù)字做占位符,最后是一個可變長度的參數(shù),來一一替換前面的占位符
- 也可以前面使用name={name}這種形式,最后一個參數(shù)是一個map,map的key即為前邊占位符的名字,map的value為參數(shù)值
第一個調用地址也可以是一個URI而不是字符串,這個時候我們構建一個URI即可,參數(shù)都包含在URI中了,如下:
@RequestMapping("/sayhello3") public String sayHello3() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/sayhello?name={name}").build().expand("王五").encode(); URI uri = uriComponents.toUri(); ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class); return responseEntity.getBody(); }
通過Spring中提供的UriComponents來構建Uri即可。
當然,服務提供者不僅可以返回String,也可以返回一個自定義類型的對象,比如服務提供者中有如下方法:
@RequestMapping(value = "/getbook1", method = RequestMethod.GET) public Book book1() { return new Book("三國演義", 90, "羅貫中", "花城出版社"); }
對于該方法可以在服務消費者中通過如下方式來調用
@RequestMapping("/book1") public Book book1() { ResponseEntity<Book> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/getbook1", Book.class); return responseEntity.getBody(); }
運行結果如下:
第二種:getForObject
getForObject函數(shù)實際上是對getForEntity函數(shù)的進一步封裝,如果你只關注返回的消息體的內容,對其他信息都不關注,此時可以使用getForObject,舉一個簡單的例子,如下:
@RequestMapping("/book2") public Book book2() { Book book = restTemplate.getForObject("http://HELLO-SERVICE/getbook1", Book.class); return book; }
getForObject也有幾個重載方法
POST請求
在RestTemplate中,POST請求可以通過如下三個方法來發(fā)起:
第一種:postForEntity 該方法和get請求中的getForEntity方法類似,如下例子:
@RequestMapping("/book3") public Book book3() { Book book = new Book(); book.setName("紅樓夢"); ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://HELLO-SERVICE/getbook2", book, Book.class); return responseEntity.getBody(); }
- 方法的第一參數(shù)表示要調用的服務的地址
- 方法的第二個參數(shù)表示上傳的參數(shù)
- 方法的第三個參數(shù)表示返回的消息體的數(shù)據(jù)類型
創(chuàng)建了一個Book對象,這個Book對象只有name屬性有值,將之傳遞到服務提供者那里去,服務提供者代碼如下:
@RequestMapping(value = "/getbook2", method = RequestMethod.POST) public Book book2(@RequestBody Book book) { System.out.println(book.getName()); book.setPrice(33); book.setAuthor("曹雪芹"); book.setPublisher("人民文學出版社"); return book; }
服務提供者接收到服務消費者傳來的參數(shù)book,給其他屬性設置上值再返回,調用結果如下:
第二種:postForObject 如果你只關注返回的消息體,可以直接使用postForObject。
用法和getForObject一致。
第三種:
postForLocation postForLocation也是提交新資源,提交成功之后,返回新資源的URI,postForLocation的參數(shù)和前面兩種的參數(shù)基本一致,只不過該方法的返回值為Uri,這個只需要服務提供者返回一個Uri即可,該Uri表示新資源的位置。
PUT請求
在RestTemplate中,PUT請求可以通過put方法調用,put方法的參數(shù)和前面介紹的postForEntity方法的參數(shù)基本一致,只是put方法沒有返回值而已。如下:
@RequestMapping("/put") public void put() { Book book = new Book(); book.setName("紅樓夢"); restTemplate.put("http://HELLO-SERVICE/getbook3/{1}", book, 99); }
DELETE請求
delete請求我們可以通過delete方法調用來實現(xiàn)
@RequestMapping("/delete") public void delete() { restTemplate.delete("http://HELLO-SERVICE/getbook4/{1}", 100); }
到此這篇關于SpringBoot中RestTemplate的使用詳解的文章就介紹到這了,更多相關RestTemplate的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- springboot中的RestTemplate使用詳解
- SpringBoot使用RestTemplate的示例詳解
- Springboot使用RestTemplate調用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請求
- 關于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務傳輸?shù)膯栴}
- Springboot RestTemplate 簡單使用解析
- SpringBoot3 RestTemplate配置與使用詳解
相關文章
使用springboot 獲取控制器參數(shù)的幾種方法小結
這篇文章主要介紹了使用springboot 獲取控制器參數(shù)的幾種方法小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12springboot項目中mybatis-plus@Mapper注入失敗問題
這篇文章主要介紹了springboot項目中mybatis-plus@Mapper注入失敗問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07