fegin设置header

第一步是实现fegin提供的RequestInterceptor接口


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: gaobingbing
 * @Date: 2019/6/12 16:43
 * @Version 1.0
 */
@Configuration
public class FeginConfig implements RequestInterceptor {
    private static final String name= "xxx";  // header 名称
    private static final String value= "xxx";  // header 值

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header(name, value);
    }
}


第二步在调用fegin的api上加上configuration = {FeginConfig.class},没用到eurake,fegin直调

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


@FeignClient(name = "远程服务名", url = "远程服务地址",
        configuration = {FeginConfig.class})
public interface xxxApi {

    @RequestMapping(value = "/a/b/c就是远程服务接口方法地址", method = RequestMethod.POST)
    List showXXXX();

}



参考链接:
https://my.oschina.net/aulbrother/blog/1610011
https://blog.csdn.net/qq_40738872/article/details/88410083

你可能感兴趣的:(fegin设置header)