当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
手撸架构,Mysql 面试126问
js cool dashboard plugin
NVIDIA NeMo Metrics 轻量性能采集系统
Pod Scheduling Strategy: Affinity, Stain and Stain Tolerance
Speed up your programs with bitwise operations
Seneor Exposure Basics
新特性解读 | MySQL 8.0 GIPK 不可见主键
pig4cloud服务架构使用
php - the first of three solid foundations
Chapter 11 Documents
OpenFeign设置header的3种方式
SQL中字符串拼接
A powerful js pop-up alert plugin
liunx基础命令讲解
基础协议讲解
Taurus.MVC V3.0.3 Microservice Open Source Framework Released: Make the evolution of .NET architecture easier in large concurrency.
1.3 Rapid Spanning Tree Protocol RSTP
PHP伪协议详解
FreeRTOS实验--删除任务
智能手表前景如何?