RestTemplate使用之如何設(shè)置請求頭、請求體
RestTemplate使用:設(shè)置請求頭、請求體
HttpEntity ?
使用 RestTemplate 時(shí)可以通過 HttpEntity 設(shè)置請求頭和請求體。HttpEntity 有4個(gè)構(gòu)造方法:
- 既不設(shè)置請求體,也不設(shè)置請求頭
- 只設(shè)置請求體
- 只設(shè)置請求頭
- 同時(shí)設(shè)置請求體和請求頭
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è)置請求體 ? 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è)置請求頭 ? 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è)置請求體與請求頭 ? this.body = body; ? HttpHeaders tempHeaders = new HttpHeaders(); ? if (headers != null) { ? ? ? tempHeaders.putAll(headers); ? } ? this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders); }
1、為 post、put 請求設(shè)置請求頭、請求體
如果是為 post、put 請求設(shè)置請求頭、請求體,可以在調(diào)用方法時(shí),利用第二個(gè)參數(shù)傳入 HttpEntity 對象,例如:
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è)置請求頭和請求體:
? ? @PostMapping("post_with_body_and_header") ? ? public void postWithBodyAndHeader(@RequestBody(required = false) UserEntity requestBody) { ? ? ? ? // 1.請求頭 ? ? ? ? HttpHeaders httpHeaders = new HttpHeaders(); ? ? ? ? httpHeaders.add("headerName1", "headerValue1"); ? ? ? ? httpHeaders.add("headerName2", "headerValue2"); ? ? ? ? httpHeaders.add("headerName3", "headerValue3"); ? ? ? ? httpHeaders.add("Content-Type", "application/json"); // 傳遞請求體時(shí)必須設(shè)置 ? ? ? ? // 2.請求頭 & 請求體 ? ? ? ? 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、為其他請求設(shè)置請求頭、請求體
如果是其它HTTP方法調(diào)用要設(shè)置請求頭,可以使用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添加請求頭
//請求添加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); //請求結(jié)果 String str = resEntity.getBody();
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中如何利用Set判斷List集合中是否有重復(fù)元素
在開發(fā)工作中,我們有時(shí)需要去判斷List集合中是否含有重復(fù)的元素,這時(shí)候我們不需要找出重復(fù)的元素,我們只需要返回一個(gè)?Boolean?類型就可以了,下面通過本文給大家介紹Java中利用Set判斷List集合中是否有重復(fù)元素,需要的朋友可以參考下2023-05-05SpringMVC獲取請求參數(shù)實(shí)現(xiàn)方法介紹
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),這篇文章主要介紹了SpringMVC實(shí)現(xiàn)獲取請求參數(shù)方法2022-11-11SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口
這篇文章主要介紹了SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Spring Data環(huán)境搭建實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Data環(huán)境搭建實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java8 使用CompletableFuture 構(gòu)建異步應(yīng)用方式
這篇文章主要介紹了Java8 使用CompletableFuture 構(gòu)建異步應(yīng)用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Springboot集成Mybatis-plus、ClickHouse實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能
本文給大家講解Springboot + mybatis-plus 集成ClickHouse,實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08