RestTemplate使用之如何設(shè)置請(qǐng)求頭、請(qǐng)求體
RestTemplate使用:設(shè)置請(qǐng)求頭、請(qǐng)求體
HttpEntity ?
使用 RestTemplate 時(shí)可以通過 HttpEntity 設(shè)置請(qǐng)求頭和請(qǐng)求體。HttpEntity 有4個(gè)構(gòu)造方法:
- 既不設(shè)置請(qǐng)求體,也不設(shè)置請(qǐng)求頭
- 只設(shè)置請(qǐng)求體
- 只設(shè)置請(qǐng)求頭
- 同時(shí)設(shè)置請(qǐng)求體和請(qǐng)求頭
HttpEntity 源碼:
/**
?* Create a new, empty {@code HttpEntity}.
?*/
protected HttpEntity() {
? this(null, null);
}
/**
?* Create a new {@code HttpEntity} with the given body and no headers.
?* @param body the entity body
?*/
public HttpEntity(T body) { // 只設(shè)置請(qǐng)求體
? this(body, null);
}
/**
?* Create a new {@code HttpEntity} with the given headers and no body.
?* @param headers the entity headers
?*/
public HttpEntity(MultiValueMap<String, String> headers) { // 只設(shè)置請(qǐng)求頭
? this(null, headers);
}
/**
?* Create a new {@code HttpEntity} with the given body and headers.
?* @param body the entity body
?* @param headers the entity headers
?*/
public HttpEntity(T body, MultiValueMap<String, String> headers) { // 同時(shí)設(shè)置請(qǐng)求體與請(qǐng)求頭
? this.body = body;
? HttpHeaders tempHeaders = new HttpHeaders();
? if (headers != null) {
? ? ? tempHeaders.putAll(headers);
? }
? this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
}1、為 post、put 請(qǐng)求設(shè)置請(qǐng)求頭、請(qǐng)求體
如果是為 post、put 請(qǐng)求設(shè)置請(qǐng)求頭、請(qǐng)求體,可以在調(diào)用方法時(shí),利用第二個(gè)參數(shù)傳入 HttpEntity 對(duì)象,例如:
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);
Book book = restTemplate.postForObject("http://127.0.0.1:8080/getbook", requestEntity, Book.class);PS:public class HttpHeaders implements MultiValueMap<String, String>, Serializable
同時(shí)設(shè)置請(qǐng)求頭和請(qǐng)求體:
? ? @PostMapping("post_with_body_and_header")
? ? public void postWithBodyAndHeader(@RequestBody(required = false) UserEntity requestBody) {
? ? ? ? // 1.請(qǐng)求頭
? ? ? ? HttpHeaders httpHeaders = new HttpHeaders();
? ? ? ? httpHeaders.add("headerName1", "headerValue1");
? ? ? ? httpHeaders.add("headerName2", "headerValue2");
? ? ? ? httpHeaders.add("headerName3", "headerValue3");
? ? ? ? httpHeaders.add("Content-Type", "application/json"); // 傳遞請(qǐng)求體時(shí)必須設(shè)置
? ? ? ? // 2.請(qǐng)求頭 & 請(qǐng)求體
? ? ? ? HttpEntity<String> fromEntity = new HttpEntity<>(JSONUtil.toJsonStr(requestBody), httpHeaders);
? ? ? ? MessageBox responseBody = restTemplate.postForObject(INVOKE_URL + "/test/receive", fromEntity, MessageBox.class);
? ? ? ? log.info("響應(yīng)體:{}", JSONUtil.toJsonPrettyStr(responseBody));
? ? }2、為其他請(qǐng)求設(shè)置請(qǐng)求頭、請(qǐng)求體
如果是其它HTTP方法調(diào)用要設(shè)置請(qǐng)求頭,可以使用exchange()方法:
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);
HttpEntity<String> response = template.exchange(
? ? ? ? "http://example.com/hotels/{hotel}",
? ? ? ? HttpMethod.GET,?
? ? ? ? requestEntity,?
? ? ? ? String.class,?
? ? ? ? "42"
? ? );
String responseHeader = response.getHeaders().getFirst("MyResponseHeader");
String body = response.getBody();RestTemplate添加請(qǐng)求頭
//請(qǐng)求添加token頭
HttpHeaders headers = new HttpHeaders();
headers.add("token",token);
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
ResponseEntity<String> resEntity = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, String.class);
//請(qǐng)求結(jié)果
String str = resEntity.getBody();總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 枚舉類和自定義枚舉類和enum聲明及實(shí)現(xiàn)接口的操作
這篇文章主要介紹了Java 枚舉類和自定義枚舉類和enum聲明及實(shí)現(xiàn)接口的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java反射獲取所有Controller和RestController類的方法
這篇文章給大家分享了Java反射獲取所有Controller和RestController類的方法,文中有詳細(xì)的代碼示例講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08
Java+Ajax實(shí)現(xiàn)的用戶名重復(fù)檢驗(yàn)功能實(shí)例詳解
這篇文章主要介紹了Java+Ajax實(shí)現(xiàn)的用戶名重復(fù)檢驗(yàn)功能,結(jié)合實(shí)例形式詳細(xì)分析了java針對(duì)用戶名提交的ajax數(shù)據(jù)庫查詢與重復(fù)檢查功能相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-12-12
SpringMVC中@ModelAttribute與@RequestBody的區(qū)別及說明
這篇文章主要介紹了SpringMVC中@ModelAttribute與@RequestBody的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
RocketMQ?offset確認(rèn)機(jī)制示例詳解
這篇文章主要為大家介紹了RocketMQ?offset確認(rèn)機(jī)制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

