SpringBoot中RestTemplate的使用詳解
RestTemplate的使用
GET請(qǐng)求
在RestTemplate中,發(fā)送一個(gè)GET請(qǐng)求,我們可以通過(guò)如下兩種方式:
第一種:
getForEntity getForEntity方法的返回值是一個(gè) ResponseEntity , ResponseEntity 是 Spring 對(duì) HTTP 請(qǐng)求響應(yīng)的封裝,包括了幾個(gè)重要的元素,如響應(yīng)碼、contentType、contentLength、響應(yīng)消息體等。比如下面一個(gè)例子:
@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 的第一個(gè)參數(shù)為要調(diào)用的服務(wù)的地址,這里調(diào)用了服務(wù)提供者提供的/hello接口,注意這里是通過(guò)服務(wù)名調(diào)用而不是服務(wù)地址,如果寫(xiě)成服務(wù)地址就沒(méi)法實(shí)現(xiàn)客戶端負(fù)載均衡了。
getForEntity 第二個(gè)參數(shù)String.class表示希望返回的body類(lèi)型是String類(lèi)型拿到返回結(jié)果之后,將返回結(jié)果遍歷打印出來(lái) 最終顯示結(jié)果如下:

有時(shí)候我在調(diào)用服務(wù)提供者提供的接口時(shí),可能需要傳遞參數(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();
}- 可以用一個(gè)數(shù)字做占位符,最后是一個(gè)可變長(zhǎng)度的參數(shù),來(lái)一一替換前面的占位符
- 也可以前面使用name={name}這種形式,最后一個(gè)參數(shù)是一個(gè)map,map的key即為前邊占位符的名字,map的value為參數(shù)值
第一個(gè)調(diào)用地址也可以是一個(gè)URI而不是字符串,這個(gè)時(shí)候我們構(gòu)建一個(gè)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();
}通過(guò)Spring中提供的UriComponents來(lái)構(gòu)建Uri即可。
當(dāng)然,服務(wù)提供者不僅可以返回String,也可以返回一個(gè)自定義類(lèi)型的對(duì)象,比如服務(wù)提供者中有如下方法:
@RequestMapping(value = "/getbook1", method = RequestMethod.GET)
public Book book1() {
return new Book("三國(guó)演義", 90, "羅貫中", "花城出版社");
}對(duì)于該方法可以在服務(wù)消費(fèi)者中通過(guò)如下方式來(lái)調(diào)用
@RequestMapping("/book1")
public Book book1() {
ResponseEntity<Book> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/getbook1", Book.class);
return responseEntity.getBody();
}運(yùn)行結(jié)果如下:

第二種:getForObject
getForObject函數(shù)實(shí)際上是對(duì)getForEntity函數(shù)的進(jìn)一步封裝,如果你只關(guān)注返回的消息體的內(nèi)容,對(duì)其他信息都不關(guān)注,此時(shí)可以使用getForObject,舉一個(gè)簡(jiǎn)單的例子,如下:
@RequestMapping("/book2")
public Book book2() {
Book book = restTemplate.getForObject("http://HELLO-SERVICE/getbook1", Book.class);
return book;
}getForObject也有幾個(gè)重載方法

POST請(qǐng)求
在RestTemplate中,POST請(qǐng)求可以通過(guò)如下三個(gè)方法來(lái)發(fā)起:
第一種:postForEntity 該方法和get請(qǐng)求中的getForEntity方法類(lèi)似,如下例子:
@RequestMapping("/book3")
public Book book3() {
Book book = new Book();
book.setName("紅樓夢(mèng)");
ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://HELLO-SERVICE/getbook2", book, Book.class);
return responseEntity.getBody();
}- 方法的第一參數(shù)表示要調(diào)用的服務(wù)的地址
- 方法的第二個(gè)參數(shù)表示上傳的參數(shù)
- 方法的第三個(gè)參數(shù)表示返回的消息體的數(shù)據(jù)類(lèi)型
創(chuàng)建了一個(gè)Book對(duì)象,這個(gè)Book對(duì)象只有name屬性有值,將之傳遞到服務(wù)提供者那里去,服務(wù)提供者代碼如下:
@RequestMapping(value = "/getbook2", method = RequestMethod.POST)
public Book book2(@RequestBody Book book) {
System.out.println(book.getName());
book.setPrice(33);
book.setAuthor("曹雪芹");
book.setPublisher("人民文學(xué)出版社");
return book;
}服務(wù)提供者接收到服務(wù)消費(fèi)者傳來(lái)的參數(shù)book,給其他屬性設(shè)置上值再返回,調(diào)用結(jié)果如下:

第二種:postForObject 如果你只關(guān)注返回的消息體,可以直接使用postForObject。
用法和getForObject一致。
第三種:
postForLocation postForLocation也是提交新資源,提交成功之后,返回新資源的URI,postForLocation的參數(shù)和前面兩種的參數(shù)基本一致,只不過(guò)該方法的返回值為Uri,這個(gè)只需要服務(wù)提供者返回一個(gè)Uri即可,該Uri表示新資源的位置。
PUT請(qǐng)求
在RestTemplate中,PUT請(qǐng)求可以通過(guò)put方法調(diào)用,put方法的參數(shù)和前面介紹的postForEntity方法的參數(shù)基本一致,只是put方法沒(méi)有返回值而已。如下:
@RequestMapping("/put")
public void put() {
Book book = new Book();
book.setName("紅樓夢(mèng)");
restTemplate.put("http://HELLO-SERVICE/getbook3/{1}", book, 99);
}DELETE請(qǐng)求
delete請(qǐng)求我們可以通過(guò)delete方法調(diào)用來(lái)實(shí)現(xiàn)
@RequestMapping("/delete")
public void delete() {
restTemplate.delete("http://HELLO-SERVICE/getbook4/{1}", 100);
}到此這篇關(guān)于SpringBoot中RestTemplate的使用詳解的文章就介紹到這了,更多相關(guān)RestTemplate的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請(qǐng)求詳解
- springboot中的RestTemplate使用詳解
- SpringBoot使用RestTemplate的示例詳解
- Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請(qǐng)求
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯?wèn)題
- Springboot RestTemplate 簡(jiǎn)單使用解析
- SpringBoot3 RestTemplate配置與使用詳解
相關(guān)文章
Spring JPA學(xué)習(xí)之delete方法示例詳解
這篇文章主要為大家介紹了Spring JPA學(xué)習(xí)delete方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
使用springboot 獲取控制器參數(shù)的幾種方法小結(jié)
這篇文章主要介紹了使用springboot 獲取控制器參數(shù)的幾種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java List分頁(yè)功能實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java List分頁(yè)功能實(shí)現(xiàn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
springboot項(xiàng)目中mybatis-plus@Mapper注入失敗問(wèn)題
這篇文章主要介紹了springboot項(xiàng)目中mybatis-plus@Mapper注入失敗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
SpringMVC實(shí)現(xiàn)自定義類(lèi)型轉(zhuǎn)換器
本篇文章主要介紹了SpringMVC實(shí)現(xiàn)自定義類(lèi)型轉(zhuǎn)換器 ,詳細(xì)的介紹了自定義類(lèi)型轉(zhuǎn)換器的用法和好處,有興趣的可以了解一下。2017-04-04

