FeignConfig调用携带本次请求的请求头信息 创建FeignConfig1234567891011121314151617181920212223242526272829303132333435363738394041import feign.RequestInterceptor;import feign.RequestTemplate;import lombok.extern.slf4j.Slf4j;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.util.Enumeration;/** * 设置Feign调用请求头信息 */@Slf4jpublic class FeignConfig implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { // try起来防止由于设备上报的信息没有请求头信息导致报错,调用失败 try { // 获取当前请求头信息 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //传递header里面所有内容 Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); // 跳过 content-length ,不跳过会导致feign.RetryableException: too many bytes written executing if (name.equals("content-length")){ continue; } // 设置请求头信息 requestTemplate.header(name, values); } } } catch (Exception e) { log.info("设备上报调用,无请求头信息"); } }} feign接口处添加注解参数1234@FeignClient(configuration = FeignConfig.class)public interface testService{ }