Feign Interceptor 拦截器实现全局请求参数

背景

  在第三方API对接中通常所有接口都需要在Header或Param放置固定参数(Token、开发者Key等),因为是SpringCloud开发,一般HTTP工具采用Feign。如果选择每个方法都copy相应字段,显得较为冗余。这个时候就可以使用Feign的Interceptor功能。

实现

Talk is cheap,show me the code.

下面选择一个具体场景,需要某个FeignClient内所有接口都带上一个查询字符串:name=Allen

@FeignClient(value = "example", url = "https://api.xxx.com",configuration = MyConfig.class)
public interface ExampleClient {

    @PutMapping(value = "/a")
    AddRes add(@RequestParam("id") String id);

   
    @DeleteMapping(value = "/b")
    DelRes delete(@RequestParam("id") String id);
}

在@FeignClient注解的configuration参数指定一个配置类,下面是这个配置类的实现

public class MyConfig {

    @Bean("myInterceptor")
    public RequestInterceptor getRequestInterceptor() {
        return new MyClientInterceptor();
    }

}

//拦截器实现
class MyClientInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.query("name","Allen") ;
    }
    
}

配置类指定我们的自定义拦截器,拦截器需要继承RequestInterceptor,实现 apply(RequestTemplate requestTemplate) 方法。在这个方法我们拿到了被拦截请求的RestTemplate实例,就可以往里面扔公共参数啦~

在这里调用query,就是追加一个新的query param,还有其他api如header、body等等,按需调用即可。

如果处理比较简单,我们可以利用Lambda直接在配置类编写匿名内部类,代码看起来简洁得多。

public class MyConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> template.query("name", "Allen");
    }
    
}

Feign可以配置多个拦截器,但Spring并不保证其执行顺序。

Zero or more {@code RequestInterceptors} may be configured for purposes such as adding headers to
all requests. No guarantees are give with regards to the order that interceptors are applied.

posted @ 2020-03-04 17:45  d1zzyboy  阅读(10036)  评论(0编辑  收藏  举报