当前位置:网站首页>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 @RequestHeaderannotation
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、实现RequestInterceptorInterface
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
边栏推荐
- MD5 detailed explanation (check file integrity)
- js炫酷仪表盘插件
- 定了!2022世界VR产业大会将继续在南昌召开
- 图神经网络(GNN)的简介「建议收藏」
- There are several ways to jump to js source code, jump on the current page, jump on the blank page
- Basic operations of openGauss database (super detailed)
- JVM学习----垃圾回收调优
- MyCat2的介绍与安装以及基本使用
- 以Boost为例的type3电压环补偿器实例
- zabbix automated monitoring script
猜你喜欢

如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?

Basic protocol explanation

SQL Server database generation and execution of SQL scripts

np.nan, np.isnan, None, pd.isnull, pd.isna 整理与小结

Process finished with exit code 1

Openlayers Quick Start Tutorial

测试开发之路,我在大厂做测试这四年的感悟

FreeRTOS creation tasks - dynamic creation, static creation

Intouch Historian历史曲线配置导入导出

SQL Server 数据库之导入导出数据
随机推荐
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
商业流程服务BPass你真的了解吗?
SQL Server 2014 installation tutorial (nanny-level graphic tutorial)
Pod调度策略:亲和性、污点与污点容忍
Import and export data of SQL Server database
0801~ Interview questions
WPF——自定义日历
新特性解读 | MySQL 8.0 GIPK 不可见主键
js true 3d histogram plugin
【第六届强网杯CTF-Wp】
MyCat2的介绍与安装以及基本使用
软件成分分析:手握5大能力守护软件供应链安全
Four seasons of trees realized by svg
SQL Server 数据库之生成与执行 SQL 脚本
ESP8266模块使用完整教程「建议收藏」
svg balloon rises explosion js special effect
unique in numpy & pandas
LeetCode_139_单词拆分
1.3快速生成树协议RSTP
FreeRTOS--Priority Experiment