当前位置:网站首页>3 ways for OpenFeign to set headers
3 ways for OpenFeign to set headers
2022-08-02 12:34:00 【m0_67394006】
Set the Header information of OpenFeign's FeignClient
When using Feign to make remote calls between microservices, you need to add information in the header, so how do SpringBoot and SpringCloud OpenFeign's @FeignClient set the header?There are 5 ways to set request header information:
- Add headers attribute to @RequestMapping annotation
- Add @RequestHeader annotation in front of method parameters
- Implement RequestInterceptor interface
Since Feign fully supports Spring MVC annotations, it is recommended to use the first two Feign settingsheader method, that is: Spring MVC uses annotations to set headers.
1. Add the headers attribute to the **@RequestMapping
** annotation
Configure in application.yml
[email protected](value = "/service/rest/v1/script/{scriptName}/run",headers = {"Authorization=Basic YWRtaW46QFdUTDE5OTIwMTE4MDI3MQ==","Content-Type=text/plain","AppSecret=${my.name}"})String runScript(@PathVariable("scriptName") String scriptName);
2. Add @RequestHeader
annotation
in front of method parametersSet a single header property
@PostMapping(value = "/service/rest/v1/script/{scriptName}/run", headers = {"Content-Type=text/plain","AppSecret=${my.name}"})
String runScript(@PathVariable("scriptName") String scriptName,@RequestHeader("Authorization") String authorization);public StringrunScript(String scriptName) {
return nexusOpenFeign.runScript(scriptName,“Basic YWRtaW46QFdUTDE5OTIwMTE4MDI3MQ==”);
}
Set multiple header properties
@PostMapping(value = “/service/rest/v1/script/{scriptName}/run”)
String runScript(@PathVariable(“scriptName”) String scriptName,@RequestHeader MultiValueMapheaders); public String runScript(String scriptName) {
MultiValueMapheaders = new HttpHeaders();
headers.put("Authorization", Collections.singletonList("Basic YWRtaW46QFdUTDE5OTIwMTE4MDI3MQ=="));
headers.add("Content-Type","text/plain");
return nexusOpenFeign.runScript(scriptName,headers);
}
3、实现RequestInterceptor
Interface
If FeignRequestInterceptor is injected into the spring container, it will take effect globally, that is to say, the configuration will take effect even in FeignClient that does not specify the configuration attribute.
By configuring @Component or @Service or @Configuration, the configuration can be injected into the spring container, and the global configuration can be implemented, so that all FeignClient feign interfaces in the project can use this configuration.
If you only want toUse this configuration for the feign interface that specifies FeignClient, do not inject this configuration into spring.
@Configurationpublic class FeignRequestInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate template) {template.header(HttpHeaders.AUTHORIZATION, "tokenVal");}}@FeignClient(url = "${user.api.url}", name = "user", configuration = FeignRequestInterceptor.class)public interface UserFeignClient {@GetMapping(value = "/simple/{id}")public User findById(@RequestParam("id") String id);}
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
LeetCode_377_Combination Sum IV
如何更好评估信用贷风险?看这场评分卡模型直播就可以了
PHP伪协议详解
liunx基础命令讲解
Taurus.MVC V3.0.3 微服务开源框架发布:让.NET 架构在大并发的演进过程更简单。
Technology sharing | Description of the electronic fence function in the integrated dispatching system
分布式限流利器,手撕&redisson实现
DTG-SSOD: The latest semi-supervised detection framework, Dense Teacher (with paper download)
1.3 Rapid Spanning Tree Protocol RSTP
Drools(8):WorkBench使用
随机推荐
numpy&pands 中的unique
不错的射击类js小游戏源码
三种实现分布式锁的方式
SQL Server 数据库之生成与执行 SQL 脚本
Basic operations of openGauss database (super detailed)
Chapter 14 Manually create a REST service (2)
js炫酷仪表盘插件
php——三篇夯实根基第一篇
Solve the problem of Chinese garbled characters in exporting excel file names
测试开发之路,我在大厂做测试这四年的感悟
30 lines of code to realize serverless real-time health code recognition -- operation manual
zabbix自动化监控脚本
Object.entries()
js九宫格样式抽奖插件
FreeRTOS--stack experiment
新特性解读 | MySQL 8.0 GIPK 不可见主键
自定义mvc框架复习
pgsql数据库实现导入导出
FreeRTOS experiment--one function creates multiple tasks
基础协议讲解