zoukankan      html  css  js  c++  java
  • & 设置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 {
    }
    
  • 相关阅读:
    CAS实战の简介
    高效程序员的45个习惯の排除万难奋勇前进
    高效程序员的45个习惯の对事不对人
    高效程序员的45个习惯の欲速则不达
    高效程序员的45个习惯の正确做事
    Java中Runnable和Thread的区别
    Intellij IDEA 14的注册机(Java版)
    session token防表单重提
    maven下@override标签失效
    Oracle sql 优化の索引监控
  • 原文地址:https://www.cnblogs.com/doagain/p/11087925.html
Copyright © 2011-2022 走看看