设置Feign的Header信息

2019-08-16 09:13:09来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

设置Feign的Header信息

设置Feign的Header信息

两种方式

1.在方法参数前面添加@RequestHeader注解

@PostMapping(value = "/getStuDetail")
public ReturnInfo getStudentDetail(@RequestBody Map map,@RequestHeader(name = "id") String id);

使用@RequestHeader(name = "id")可以传递动态header属性

2.实现RequestInterceptor接口,设置Header(所有Feign请求)

import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import feign.RequestInterceptor;
import feign.RequestTemplate;

@Configuration
public class FeignConfiguration implements RequestInterceptor {???
      @Override??? 
      public void apply(RequestTemplate template) {???????
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();???????
            HttpServletRequest request = attributes.getRequest();??????? 
            Enumeration<String> headerNames = request.getHeaderNames();???????
            if (headerNames != null) {???????????
                  while (headerNames.hasMoreElements()) {????????????
                        String name = headerNames.nextElement();?????????????
                        String values = request.getHeader(name);????????????
                        template.header(name, values);??????????? 
                  }??????????? 
            }???
      }
}
//configuration = FeignConfiguration.class===================
@Component@FeignClient(value = "xxxxxxxxx",fallback = XxxxxServiceHystric.class ,configuration = FeignConfiguration.class)
public interface XxxxxService {
}

扫码关注公众号 领取精品视频学习资源


原文链接:https://www.cnblogs.com/doagain/p/11087925.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:关于Java的三种普通排序

下一篇:JAVA基础之网络通信协议--TCP与UDP