当前位置:网站首页>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
边栏推荐
- To eliminate air bubbles to save the mushroom h5 small game source code
- FreeRTOS中名称规范
- String concatenation in SQL
- NVIDIA NeMo Metrics 轻量性能采集系统
- OpenFeign设置header的3种方式
- The 7 most commonly used data analysis thinking, solve 95% of the analysis problems
- LeetCode_377_组合总和Ⅳ
- Chapter 14 Manually create a REST service (2)
- SQL Server 2014 installation tutorial (nanny-level graphic tutorial)
- MyCat2的介绍与安装以及基本使用
猜你喜欢
随机推荐
FreeRTOS--Priority Experiment
Technology sharing | Description of the electronic fence function in the integrated dispatching system
Data Lake (3): Hudi Concept Terminology
手撸架构,网络 面试36问
np.nan, np.isnan, None, pd.isnull, pd.isna finishing and summary
SQL Server 2014安装教程(保姆级图解教程)
linux basic command explanation
MFC入门教程(深入浅出MFC)
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
svg实现的树木四季变化
After Effects 教程,如何在 After Effects 中对蒙版进行动画绘制?
How to use the database like tap water?|Tencent Cloud Database TDSQL-C
一款强大的js弹出alert插件
手撸架构,MongDB 面试50问
There are several ways to jump to js source code, jump on the current page, jump on the blank page
MyCat2的介绍与安装以及基本使用
js stopwatch countdown plugin
0801~ Interview questions
SQL Server如何建表
定了!2022世界VR产业大会将继续在南昌召开








