Spring-cloud-openfeign拦截器RequestInterceptor接口

RequestInterceptor接口位于包io.github.openfeign-core下,使用Spring Cloud Feign的时候会自动依赖这个包

下面的代码会在每次调用Feign1的m1方法时,向HTTP头追加键值对武汉3:晴川历历汉阳树

@FeignClient(value = "feignA", url = "XXX或者el表达式${abc:默认值}")
public interface Feign1 {

    @GetMapping(value = "/api/xxx", headers = {
            "武汉1=杨泗港长江大桥",
            "武汉2=鹦鹉洲长江大桥"})
    String m1();
}
package 你的包名;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        String name = template.feignTarget().name();
        if ("feignA".equals(name)) {
        	// 每次调用Feign1的m1方法时,都会往HTTP头追加武汉3=晴川历历汉阳树
            template.header("武汉3", "晴川历历汉阳树");
        }
    }
}

你可能感兴趣的:(SpringCloud,spring,cloud)