feignclient 添加http 请求头参数
时间: 2023-10-09 21:17:34 浏览: 94
在使用 FeignClient 发起 HTTP 请求时,可以通过配置 RequestInterceptor 实现添加请求头参数的功能。具体步骤如下:
1. 创建 RequestInterceptor 实现类,实现 apply(RequestTemplate) 方法,添加需要的请求头参数,例如:
```
public class MyRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.header("Authorization", "Bearer your-token");
}
}
```
2. 在使用 FeignClient 的接口上添加 @FeignClient 注解时,通过 configuration 属性指定配置类,例如:
```
@FeignClient(name = "example", url = "http://example.com", configuration = MyFeignConfiguration.class)
public interface MyFeignClient {
// ...
}
```
3. 创建 MyFeignConfiguration 配置类,将 MyRequestInterceptor 实例添加到 Feign 的拦截器列表中,例如:
```
@Configuration
public class MyFeignConfiguration {
@Autowired
private MyRequestInterceptor myRequestInterceptor;
@Bean
public RequestInterceptor requestInterceptor() {
return myRequestInterceptor;
}
}
```
完成以上步骤后,使用 FeignClient 发起的 HTTP 请求就会自动添加指定的请求头参数了。
阅读全文
相关推荐

















