springCloud 使用feign複製請求頭調用其餘服務 content-length不一致致使調用失敗

背景:目前須要前端須要從B系統中獲取數據,請求先發送到A系統,而後由A系統使用Feign調用B系統的接口,因爲調用B系統時須要將請求的用戶相關信息一塊兒帶到B系統,因此是用的Feign的請求攔截對其進行請求頭複製前端

如下是請求頭複製的攔截器java

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(重點),最初浮現的問題是web

 

 

 在postMan寫入的json不規範致使請求B系統報錯spring

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

若是規範就沒問題apache

 

 

 通過屢次踩坑才知道特喵的 content-length是由body裏面字符數控制的json

 

因此問題明瞭了,貼上解決後的攔截器app

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詳解參考文章 :http://www.javashuo.com/article/p-ofagiidh-bu.htmlsocket

相關文章
相關標籤/搜索