0
点赞
收藏
分享

微信扫一扫

spring boot 添加请求头

在Spring Boot应用程序中,可以通过多种方式添加请求头(Request Headers)。以下是一些常见的方法:

1. 使用RestTemplate添加请求头

RestTemplate是Spring提供的用于同步客户端HTTP访问的模板工具类。可以通过HttpHeadersHttpEntity来设置请求头。

 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 static void main(String[] args) {  
 
         RestTemplate restTemplate = new RestTemplate();  
 
   
 
         // 创建HttpHeaders对象并添加请求头  
 
         HttpHeaders headers = new HttpHeaders();  
 
         headers.set("Authorization", "Bearer YOUR_TOKEN");  
 
         headers.set("Custom-Header", "CustomHeaderValue");  
 
   
 
         // 创建HttpEntity对象  
 
         HttpEntity<String> entity = new HttpEntity<>("parameters", headers);  
 
   
 
         // 发送请求  
 
         String url = "https://api.example.com/resource";  
 
         ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);  
 
   
 
         // 打印响应  
 
         System.out.println(response.getBody());  
 
     }  
 
 }

2. 使用WebClient添加请求头

WebClient是Spring WebFlux提供的非阻塞、响应式的HTTP客户端。

java复制代码
 import org.springframework.web.reactive.function.client.WebClient;  
 
 import reactor.core.publisher.Mono;  
 
   
 
 public class WebClientExample {  
 
   
 
     public static void main(String[] args) {  
 
         WebClient webClient = WebClient.builder()  
 
                 .baseUrl("https://api.example.com")  
 
                 .defaultHeader("Authorization", "Bearer YOUR_TOKEN")  
 
                 .defaultHeader("Custom-Header", "CustomHeaderValue")  
 
                 .build();  
 
   
 
         Mono<String> response = webClient.get()  
 
                 .uri("/resource")  
 
                 .retrieve()  
 
                 .bodyToMono(String.class);  
 
   
 
         // 打印响应  
 
         response.block(); // 注意:block() 只在阻塞场景中使用,例如在main方法中  
 
     }  
 
 }

3. 在控制器方法中添加响应头

如果需要在Spring Boot控制器中添加响应头,可以直接在方法中使用HttpServletResponse对象,或者使用ResponseEntity

使用HttpServletResponse

 import javax.servlet.http.HttpServletResponse;  
 
 import org.springframework.web.bind.annotation.GetMapping;  
 
 import org.springframework.web.bind.annotation.RestController;  
 
   
 
 @RestController  
 
 public class MyController {  
 
   
 
     @GetMapping("/resource")  
 
     public String getResource(HttpServletResponse response) {  
 
         response.setHeader("Custom-Header", "CustomHeaderValue");  
 
         return "Response Body";  
 
     }  
 
 }

使用ResponseEntity

 import org.springframework.http.HttpHeaders;  
 
 import org.springframework.http.HttpStatus;  
 
 import org.springframework.http.ResponseEntity;  
 
 import org.springframework.web.bind.annotation.GetMapping;  
 
 import org.springframework.web.bind.annotation.RestController;  
 
   
 
 @RestController  
 
 public class MyController {  
 
   
 
     @GetMapping("/resource")  
 
     public ResponseEntity<String> getResource() {  
 
         HttpHeaders headers = new HttpHeaders();  
 
         headers.set("Custom-Header", "CustomHeaderValue");  
 
   
 
         return new ResponseEntity<>("Response Body", headers, HttpStatus.OK);  
 
     }  
 
 }

4. 使用拦截器添加全局请求头

如果需要在全局范围内为所有请求添加请求头,可以使用Spring的拦截器(Interceptor)。

 import javax.servlet.http.HttpServletRequest;  
 
 import javax.servlet.http.HttpServletResponse;  
 
 import org.springframework.stereotype.Component;  
 
 import org.springframework.web.servlet.HandlerInterceptor;  
 
   
 
 @Component  
 
 public class CustomInterceptor implements HandlerInterceptor {  
 
   
 
     @Override  
 
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
 
         request.setAttribute("Custom-Header", "CustomHeaderValue");  
 
         // 或者通过response添加响应头  
 
         // response.setHeader("Custom-Response-Header", "CustomResponseHeaderValue");  
 
         return true;  
 
     }  
 
 }

然后,你需要将拦截器注册到Spring MVC配置中:

 import org.springframework.beans.factory.annotation.Autowired;  
 
 import org.springframework.context.annotation.Configuration;  
 
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;  
 
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;  
 
   
 
 @Configuration  
 
 public class WebConfig implements WebMvcConfigurer {  
 
   
 
     @Autowired  
 
     private CustomInterceptor customInterceptor;  
 
   
 
     @Override  
 
     public void addInterceptors(InterceptorRegistry registry) {  
 
         registry.addInterceptor(customInterceptor);  
 
     }  
 
 }

通过这些方法,可以在Spring Boot应用程序中灵活地添加请求头和响应头。

举报

相关推荐

0 条评论