1. 添加拦截器
//在创建完成Feign.Builder对象后,添加一个拦截器到Feign.Builder中
//该拦截器可添加自定义请求参数,每次feign接口调用被触发时,都会经过该拦截器
class BeanPostProcessor implements BeanPostProcessor {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
@Qualifier("com.xxx.RequestInterceptor")
private RequestInterceptor requestInterceptor;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//判断当前对象是否为Feign.Builder
if (bean instanceof Feign.Builder) {
Feign.Builder feignBuilder = (Feign.Builder) bean;
feignBuilder.requestInterceptor(this.requestInterceptor);
}
return bean;
}
}
2. 拦截器
//feign 拦截器
class RequestInterceptor implements feign.RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
//在feign请求头中加入自定义参数
template.header("key", "value");
}
}