Spring中如何为RestTemplate的所有HTTP请求添加统一查询参数?
当然可以搞定!Spring的RestTemplate提供了拦截器机制,能帮你统一给所有请求追加查询参数,再也不用在每个请求里重复写os_authType=basic了,下面给你具体的实现方案:
统一给RestTemplate请求追加查询参数的方案
1. 自定义请求拦截器
我们可以实现ClientHttpRequestInterceptor接口,在每个请求发送前修改URI,自动追加指定的查询参数。不管你用RestTemplate发GET、POST还是其他请求,参数都会被自动加上。
代码示例:
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.web.util.UriComponentsBuilder; import java.io.IOException; import java.util.Map; public class CustomQueryParamInterceptor implements ClientHttpRequestInterceptor { private final Map<String, String> queryParams; // 构造函数传入要统一添加的查询参数,灵活适配不同场景 public CustomQueryParamInterceptor(Map<String, String> queryParams) { this.queryParams = queryParams; } @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { // 基于原始URI构建新的URI,追加查询参数 UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(request.getURI()); queryParams.forEach(uriBuilder::queryParam); // 创建新的请求对象,替换修改后的URI HttpRequest modifiedRequest = new HttpRequest() { @Override public String getMethodValue() { return request.getMethodValue(); } @Override public URI getURI() { return uriBuilder.build().toUri(); } @Override public HttpHeaders getHeaders() { return request.getHeaders(); } }; // 继续执行请求流程 return execution.execute(modifiedRequest, body); } }
2. 配置RestTemplate并注册拦截器
接下来在你的MyService里,通过RestTemplateBuilder把拦截器注册进去。考虑到你提到的os_authType=basic通常需要配合Basic认证,这里顺便把认证配置也加上,一步到位:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.boot.web.client.RestTemplateBuilder; import java.util.Collections; @Service public class MyService { private final RestTemplate restTemplate; @Autowired public MyService(RestTemplateBuilder restTemplateBuilder, @Value("${api.username}") final String username, @Value("${api.password}") final String password) { // 定义要统一追加的查询参数 var queryParams = Collections.singletonMap("os_authType", "basic"); // 配置RestTemplate:添加Basic认证 + 注册自定义拦截器 restTemplate = restTemplateBuilder .basicAuthentication(username, password) .additionalInterceptors(new CustomQueryParamInterceptor(queryParams)) .build(); } // 后续用这个restTemplate发请求,都会自动带上?os_authType=basic参数 public void fetchAtlassianData() { // 示例请求,无需手动拼接查询参数 String response = restTemplate.getForObject("https://your-atlassian-api-endpoint", String.class); // ...处理业务逻辑 } }
补充说明
- 这个拦截器对所有HTTP方法都生效,因为它是修改请求URI,和请求类型无关;
- 如果需要添加多个查询参数,把
singletonMap换成普通HashMap,传入多组键值对即可; - 要是不需要Basic认证,直接去掉
.basicAuthentication(username, password)这一行就行,只保留拦截器配置。
这样一来,你所有通过这个RestTemplate发起的请求,都会自动带上os_authType=basic参数,再也不用在代码各处重复配置啦!
内容的提问来源于stack exchange,提问作者user6605797
相关产品推荐
相关产品推荐

