zoukankan      html  css  js  c++  java
  • Spring Cloud Feign 调用微服务传递header请求头

    package com.chitic.module.core.config;
    
    import feign.RequestInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Enumeration;
    
     
    @Configuration
    public class FeignConfig {
    
        @Bean
        public RequestInterceptor headerInterceptor() {
            return template -> {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (null != attributes) {
                    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);
                        }
                    }
                }
            };
        }
    
    }

    需注意,feign调用时不能调用含有HttpServletResponse参数(比如常用的数据导出),以下就不能远程调用,目前没找到解决办法

  • 相关阅读:
    Python
    Python
    Python
    Flask
    记一次Orika使用不当导致的内存溢出
    SpringBoot博客开发之AOP日志处理
    SpringBoot数据访问之整合mybatis注解版
    Blazor WebAssembly 应用程序中进行 HTTP 请求
    Blazor Server 应用程序中进行 HTTP 请求
    MySQL数据库主从数据对比
  • 原文地址:https://www.cnblogs.com/gaomanito/p/12515526.html
Copyright © 2011-2022 走看看