Java 中使用RestTemplate传递 HTTP Header 有多种方式以下是常用的几种方法1. 使用HttpHeaders和HttpEntity推荐java复制import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class RestTemplateExample { public void sendRequestWithHeaders() { RestTemplate restTemplate new RestTemplate(); // 创建请求头 HttpHeaders headers new HttpHeaders(); headers.set(Authorization, Bearer your_token_here); headers.set(Content-Type, application/json); headers.set(X-Custom-Header, custom_value); // 如果有多个相同名称的header使用add headers.add(X-Request-ID, uuid-123); // 创建请求实体body可以是null对于GET请求 HttpEntityString entity new HttpEntity(headers); // 发送GET请求 ResponseEntityString response restTemplate.exchange( https://api.example.com/data, HttpMethod.GET, entity, String.class ); String result response.getBody(); } }2. POST 请求带 Header 和 Bodyjava复制public void postWithHeaders() { RestTemplate restTemplate new RestTemplate(); HttpHeaders headers new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setBearerAuth(your_jwt_token); // 设置Bearer Token // 请求体 String jsonBody {\name\:\test\,\value\:123}; // 组合header和body HttpEntityString entity new HttpEntity(jsonBody, headers); ResponseEntityString response restTemplate.postForEntity( https://api.example.com/create, entity, String.class ); }3. 使用ClientHttpRequestInterceptor全局配置如果需要为所有请求添加相同的 Header如统一的认证信息java复制import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.support.HttpRequestWrapper; public class RestTemplateConfig { public RestTemplate createRestTemplateWithInterceptor() { RestTemplate restTemplate new RestTemplate(); // 添加拦截器 restTemplate.getInterceptors().add((request, body, execution) - { request.getHeaders().set(Authorization, Bearer getToken()); request.getHeaders().set(X-API-Key, your_api_key); return execution.execute(request, body); }); return restTemplate; } private String getToken() { // 获取token的逻辑 return dynamic_token; } }4. 使用RequestCallback更底层控制java复制public void requestWithCallback() { RestTemplate restTemplate new RestTemplate(); RequestCallback requestCallback request - { request.getHeaders().set(Authorization, Bearer token123); // 可以在这里写入body }; ResponseExtractorString responseExtractor response - new String(response.getBody().readAllBytes()); String result restTemplate.execute( https://api.example.com/data, HttpMethod.GET, requestCallback, responseExtractor ); }5. 使用UriComponentsBuilder Headerjava复制public void getWithParamsAndHeaders() { RestTemplate restTemplate new RestTemplate(); // 构建URL String url UriComponentsBuilder .fromHttpUrl(https://api.example.com/search) .queryParam(page, 1) .queryParam(size, 10) .toUriString(); HttpHeaders headers new HttpHeaders(); headers.set(Accept, application/json); HttpEntityVoid entity new HttpEntity(headers); ResponseEntityString response restTemplate.exchange( url, HttpMethod.GET, entity, String.class ); }常用 Header 设置方法表格复制方法说明headers.set(key, value)设置单个header覆盖已有headers.add(key, value)添加header支持多值headers.setContentType(MediaType)设置Content-Typeheaders.setBearerAuth(token)设置Bearer认证headers.setBasicAuth(username, password)设置Basic认证headers.setAll(Map)批量设置注意事项Spring Boot 3.x / Spring 6RestTemplate已被标记为弃用建议迁移到WebClient响应式或RestClientSpring 6.1线程安全RestTemplate是线程安全的建议配置为单例编码问题中文内容建议设置MediaType.APPLICATION_JSON_UTF8