Spring Boot中使用Feign设置请求头Header的值

背景

使用Feign调用外部接口时,需要在Headers中带一个如下的参数:
在这里插入图片描述
按照网上的说法,有通过注解@Header的方式; 有在@RequestMapping中设置header属性的方式.; 有通过拦截器实现header值的填充. 亲自尝试过之后,第一种方法不起作用,第二种方法不支持动态参数(发现一篇博客,这篇博客的方案二讲了如何使用动态参数,但没有亲自尝试.). 第三种会拦截所有的请求,不优雅难以根据不同的服务方提供不同的 header.

似乎陷入了死胡同,但是我突然想到:Feign是完全支持Spring MVC注解的,那我是不是只要搜索一下在
Spring MVC中如何使用注解设置header不就可以了吗.

@RequestMapping(value = "/user", method = RequestMethod.GET)
Response authentication(@RequestHeader(name = "Cookie") String header);

问题解决.

Spring Boot 中启用 Feign 客户端需要以下几个步骤,主要是通过添加依赖和使用注解来激活 Feign 的功能。下面是一个完整的指南,包括代码示例和说明。 --- ## ✅ 步骤一:添加依赖 首先,确保你的 `pom.xml`(如果是 Maven 项目)中包含 Spring Cloud 的 Feign 依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 如果你使用的是 Spring Cloud Alibaba,则还需要加上 Feign 的适配器: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> ``` --- ## ✅ 步骤二:启用 Feign 客户端 在 Spring Boot 的主启动类上添加 `@EnableFeignClients` 注解,以启用 Feign 客户端功能。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 你也可以指定 Feign 客户端所在的包路径: ```java @EnableFeignClients(basePackages = "com.example.feign.client") ``` --- ## ✅ 步骤三:定义 Feign 客户端接口 例如,定义一个 Feign 客户端接口 `RemoteCellConfService`: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import java.util.List; @FeignClient(name = "system-service", url = "http://system-service", fallbackFactory = RemoteCellConfFallbackFactory.class) public interface RemoteCellConfService { @PostMapping("/conf/getList") R<List<RlCellConf>> getList(@RequestBody RlCellConf rlCellConf, @RequestHeader("from-source") String source); } ``` --- ## ✅ 步骤四:定义熔断降级类(可选) ```java import org.springframework.stereotype.Component; @Component public class RemoteCellConfFallbackFactory implements FallbackFactory<RemoteCellConfService> { @Override public RemoteCellConfService create(Throwable cause) { return new RemoteCellConfService() { @Override public R<List<RlCellConf>> getList(RlCellConf rlCellConf, String source) { return R.fail("远程服务调用失败: " + cause.getMessage()); } }; } } ``` --- ## ✅ 步骤五:在 Service 中注入并使用 Feign 客户端 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class CellConfService { @Autowired private RemoteCellConfService remoteCellConfService; public void fetchCellConf() { RlCellConf query = new RlCellConf(); query.setExportType("excel"); R<List<RlCellConf>> result = remoteCellConfService.getList(query, "mobile-app"); System.out.println(result.getData()); } } ``` --- ## ✅ 注意事项: - Feign 默认使用 `Spring MVC` 风格的注解(如 `@PostMapping`, `@RequestBody`)。 - Feign 需要配合服务注册中心(如 Nacos、Eureka)使用,以便自动发现服务。 - 如果需要启用日志打印,可以在 `application.yml` 中添加: ```yaml logging: level: com.example.feign.client: DEBUG ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值