RestTemplate get请求,header设置及传参过程
更新时间:2026年02月12日 09:30:27 作者:涛哥是个大帅比
文章介绍了在SpringBoot中使用RestTemplate进行GET请求时的请求头设置及传参方式,包括无参数无请求头、有请求头无参数和有请求头有参数三种情况,并提供了相应的RestTemplate代码示例
前言
Spring Boot RestTemplate使用get请求,请求头header的设置及传参方式
- 1. 有参数,没有请求头
- 2. 有请求头,没参数
- 3. 有请求头,有参数
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 = "测试";
//参数
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. 有参数,没有请求头,拼接方式
String result1 = restTemplate.getForObject(url + "?id="+id+"&name="+name, String.class);
//2. 有参数,没有请求头,占位符方式
String result2 = restTemplate.getForObject(url + "?id={id}&name={name}", String.class, param);
//3. 有请求头,没参数,result3.getBody()获取响应参数
ResponseEntity<String> result3 = restTemplate.exchange(url, HttpMethod.GET, formEntity, String.class);
//4. 有请求头,有参数,result4.getBody()获取响应参数
ResponseEntity<String> result4 = restTemplate.exchange(url+"?id="+id+"&name="+name, HttpMethod.GET, formEntity, String.class);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot+ResponseBodyEmitter实时异步流式推送的实现
本文主要介绍了SpringBoot+ResponseBodyEmitter实时异步流式推送的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2025-11-11
Java中ReentrantReadWriteLock读写锁的实现
本文主要介绍了ReentrantReadWriteLock读写锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2025-05-05
Java 中的 getDeclaredFields()使用与原理解析
在Java反射机制中,getDeclaredFields()用于获取类的所有字段,包括私有字段,通过反射,可以在运行时动态地获取类的信息并操作其成员,本文详细介绍了getDeclaredFields()的使用方法、工作原理以及最佳实践,涵盖了反射的基本概念、使用场景和注意事项,感兴趣的朋友一起看看吧2025-01-01
Java Properties简介_动力节点Java学院整理
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置2017-05-05


最新评论