OpenFeignCLient 拦截器 RequestInterceptor 使用笔记

使用步骤

  1. 启动类中添加注解 @EnableFeignClients 开启feign扫描
@Slf4j
@SpringBootApplication
@EnableFeignClients
@MapperScan(basePackages = "cn.zhouxr.work.test001.mapper")
@ServletComponentScan
@EnableDiscoveryClient
@Import(SwaggerConf.class)
public class Test2Application {
    public static void main(String[] args) {
        SpringApplication.run(Test2Application.class,args);
        log.info("\n ************************* Test2Application 启动成功!! *************************\n");
    }
}
  1. 添加feign, @FeignClient 的讲解往下看.
/**
 * feignClient 参数说明
 * url: 如果url不为空的话, 则会进行http调用 , 否则是微服务之间的内部调用
 * path: 前缀
 * configuration: 为当前feign添加的配置类
 * 
 */
@FeignClient(value = "testFeignClient",url = "http://127.0.0.1" , path = "/center/api")
public interface TestFeignClient {

    @GetMapping("/evaluate/list")
    Object list(@RequestParam("category") String category);
}
  1. 添加拦截器 实现 RequestInterceptor 接口
@Slf4j
public class MyInterceptor2 implements RequestInterceptor {

    private static final String interceptionFeignName = "testFeignClient";

    @Override
    public void apply(RequestTemplate template) {
        log.info("拦截器:{}",this.getClass().getName());
        log.info(JSON.toJSONString(template));
        String feignName = template.feignTarget().name();
        if (StringUtils.equals(interceptionFeignName,feignName)){
            template.header("aa","aaa");
        }
    }
}

多个拦截器之间的执行顺序

可以通过 @Order(2) 注解控制

@Slf4j
@Component
@Order(2)
public class myInterceptor2 implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        log.info("拦截器:{}",this.getClass().getName());
        log.info(JSON.toJSONString(template));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值