当前位置:   article > 正文

java:FeignClient通过RequestInterceptor自动添加header_requestinterceptor header

requestinterceptor header

示例代码

【pom.xml】

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.9.RELEASE</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

【application.properties】

server.port=8080
spring.application.name=myFeignClient

management.server.port=7001
management.endpoints.web.exposure.include=*

logging.level.root=info
logging.level.com.chz.myFeignClient=DEBUG
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

【FeignConfiguration.java】

package com.chz.myFeignClient.config;

@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

【TestController.java】

package com.chz.myFeignClient.controller;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestBaiduFeignClient testBaiduFeignClient;

    @GetMapping("/test")
    public String test() {
        return testBaiduFeignClient.testGet();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

【TestBaiduFeignClient.java】

package com.chz.myFeignClient.feign;

@FeignClient(
        name = "testBaiduFeignClient",
        contextId = "com.chz.myFeignClient.feign.TestFeignClient",
        url = "http://www.baidu.com"
)
public interface TestBaiduFeignClient
{
    @GetMapping(value = "/")
    String testGet();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

【FeignRequestInterceptor.java】

package com.chz.myFeignClient.interceptor;

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor
{
    @Override
    public void apply(RequestTemplate template)
    {
        // 这里给每一个FeignClient的调用增加一个header
        template.header("chz-new-header", "just-for-test");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

【MyFeignClientTest.java】

package com.chz.myFeignClient;

@EnableFeignClients
@SpringBootApplication
public class MyFeignClientTest 
{
    public static void main(String[] args) {
        SpringApplication.run(MyFeignClientTest.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

启动【MyFeignClientTest】,然后访问【http://localhost:8080/test/test】
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/924294
推荐阅读
相关标签
  

闽ICP备14008679号