RestTemplate get請求,header設(shè)置及傳參過程
更新時間:2026年02月12日 09:30:27 作者:濤哥是個大帥比
文章介紹了在SpringBoot中使用RestTemplate進行GET請求時的請求頭設(shè)置及傳參方式,包括無參數(shù)無請求頭、有請求頭無參數(shù)和有請求頭有參數(shù)三種情況,并提供了相應的RestTemplate代碼示例
前言
Spring Boot RestTemplate使用get請求,請求頭header的設(shè)置及傳參方式
- 1. 有參數(shù),沒有請求頭
- 2. 有請求頭,沒參數(shù)
- 3. 有請求頭,有參數(shù)
RestTemplate
代碼如下:
package com.xinghuo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* 測試restTemplate
*/
@RestController
@Api(tags = "測試")
@RequestMapping("/test")
public class HttpController{
@Autowired
private RestTemplate restTemplate;
/**
* 測試
*/
@RequestMapping(value = "/http", method = RequestMethod.GET)
@ApiOperation(value = "測試http")
public String http() {
String id = "52db70d13ad74b0f85142e39b32164b4";
String name = "測試";
//參數(shù)
MultiValueMap<String, Object> param = new LinkedMultiValueMap<String, Object>();
param.add("id", id);
param.add("name", name);
//請求頭
HttpHeaders headers = new HttpHeaders();
headers.add("accessToken", "3d40e41e9d764d30a9a4d72e61ad61b9");
//封裝請求頭
HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
try {
//訪問地址
String url = "http://localhost:8080/testservice/test/get";
//1. 有參數(shù),沒有請求頭,拼接方式
String result1 = restTemplate.getForObject(url + "?id="+id+"&name="+name, String.class);
//2. 有參數(shù),沒有請求頭,占位符方式
String result2 = restTemplate.getForObject(url + "?id={id}&name={name}", String.class, param);
//3. 有請求頭,沒參數(shù),result3.getBody()獲取響應參數(shù)
ResponseEntity<String> result3 = restTemplate.exchange(url, HttpMethod.GET, formEntity, String.class);
//4. 有請求頭,有參數(shù),result4.getBody()獲取響應參數(shù)
ResponseEntity<String> result4 = restTemplate.exchange(url+"?id="+id+"&name="+name, HttpMethod.GET, formEntity, String.class);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Java?restTemplate發(fā)送get請求query參數(shù)傳遞問題解決
- RestTemplate get請求攜帶headers自動拼接參數(shù)方式
- restTemplate發(fā)送get與post請求并且?guī)?shù)問題
- 關(guān)于RestTemplate中的Get請求
- RestTemplate調(diào)用POST和GET請求示例詳解
- RestTemplate Get請求實現(xiàn)bean參數(shù)傳遞詳解
- RestTemplate發(fā)送HTTP?GET請求使用方法詳解
- RestTemplate實現(xiàn)發(fā)送帶headers的GET請求
- spring boot RestTemplate 發(fā)送get請求的踩坑及解決
相關(guān)文章
SpringMVC中ModelAndView用法小結(jié)
本文主要介紹了SpringMVC中ModelAndView用法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12

