当前位置:网站首页>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
边栏推荐
- 1.3 Rapid Spanning Tree Protocol RSTP
- Object.entries()
- sql concat() function
- pytorch模型转tensorflow模型
- FreeRTOS中名称规范
- 软件成分分析:手握5大能力守护软件供应链安全
- #Summer Challenge#[FFH] OpenHarmony Device Development Foundation (3) Compilation Dependencies
- Distributed current limiting, hand & redisson implementation
- SQL Server 2019安装错误0x80004005 服务没有及时响应启动或控制请求详细解决方法
- SQL Server修改数据
猜你喜欢
随机推荐
Lexicon 27 - Remove Elements - Simple Questions
Set proxy server (Google+IE) "Recommended Collection"
Js scratchable latex style draw plug-in
第十四章 手动创建 REST 服务(二)
zabbix自动化监控脚本
unique in numpy & pandas
openGauss数据库基本操作(超详细)
SQL Server 数据库之导入导出数据
Data Lake (3): Hudi Concept Terminology
Hand rolled architecture, 41 Redis interview asked
js源码跳转的几种方式,在当前页面跳转,在空白页跳转
Intouch Historian历史曲线配置导入导出
智能图像分析-智能家用电器图像目标检测统计计数检测与识别-艾科瑞特科技(iCREDIT)
zabbix automated monitoring script
Chapter 11 Documents
FreeRTOS实验--删除任务
第11章 文件
Swiper系列之轮播图
liunx基础命令讲解
js真3d柱状图插件









