背景:目前需要前端需要从B系统中获取数据,请求先发送到A系统,然后由A系统使用Feign调用B系统的接口,由于调用B系统时需要将请求的用户相关信息一起带到B系统,所以是用的Feign的请求拦截对其进行请求头复制
以下是请求头复制的拦截器
package cn.rivamed.hvc.filter; import feign.RequestInterceptor; import feign.RequestTemplate; 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; import java.util.LinkedHashMap; import java.util.Map; /** * 描述: TODO * 公司 北京瑞华康源科技有限公司 * 版本 rivamed2019 * * @version V2.0.1 * @author: 左健宏 * @date: 2019-12-20 13:08 */ @Configuration public class FeginInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { try { Map<String,String> headers = getHeaders(); for(String headerName : headers.keySet()){ requestTemplate.header(headerName, headers.get(headerName)); } }catch (Exception e){ e.printStackTrace(); } } private Map<String, String> getHeaders(){ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Map<String, String> map = new LinkedHashMap<>(); Enumeration<String> enumeration = request.getHeaderNames(); while (enumeration.hasMoreElements()) { String key = enumeration.nextElement(); String value = request.getHeader(key); // if ("content-length".equals(key)) { // continue; // } map.put(key, value); } return map; } }
该拦截器复制了所有请求头,包括content-length(重点),最初浮现的问题是
在postMan写入的json不规范导致请求B系统报错
org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is org.apache.catalina.connector.ClientAbortException: java.io.EOFException: Unexpected EOF read on the socket
如果规范就没问题
经过多次踩坑才知道特喵的 content-length是由body里面字符数控制的
所以问题明了了,贴上解决后的拦截器
package cn.rivamed.hvc.filter; import feign.RequestInterceptor; import feign.RequestTemplate; 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; import java.util.LinkedHashMap; import java.util.Map; /** * 描述: TODO * 公司 北京瑞华康源科技有限公司 * 版本 rivamed2019 * * @version V2.0.1 * @author: 左健宏 * @date: 2019-12-20 13:08 */ @Configuration public class FeginInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { try { Map<String,String> headers = getHeaders(); for(String headerName : headers.keySet()){ requestTemplate.header(headerName, headers.get(headerName)); } }catch (Exception e){ e.printStackTrace(); } } private Map<String, String> getHeaders(){ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Map<String, String> map = new LinkedHashMap<>(); Enumeration<String> enumeration = request.getHeaderNames(); while (enumeration.hasMoreElements()) { String key = enumeration.nextElement(); String value = request.getHeader(key); if ("content-length".equals(key)) { continue; } map.put(key, value); } return map; } }
content-length详解参考文章 :https://juejin.im/post/5d772cb4e51d453b5f1a0502