• & 设置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 {
    }
    
  • 相关阅读:
    nullptr和NULL
    tmux用于恢复远程屏幕
    如何改变git的默认路径
    scp拷贝文件
    C++头文件<bits/stdc++.h>
    MAME模拟器使用简单教程
    typescript接口扩展
    Typescript中的可索引接口 类类型接口
    typescript函数类型接口
    typescript静态属性 静态方法 抽象类 多态
  • 原文地址:https://www.cnblogs.com/doagain/p/11087925.html
Copyright © 2020-2023  润新知