当我们用feign发起服务调用时,如果请求头content-length设置的比实际body的长度要短,那么就会报上面这个错,反之如果设置的content-length长度比实际的body长度要长,那么会报Incomplete output stream executing POST http://xxx; 解决方案就是在通过拦截器设置feign调用的请求头时,将content-length这个请求头去掉。
@Bean public RequestInterceptor requestInterceptor() { return template -> { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); if (!name.equals("content-length")) { template.header(name, values); } } } } }; }
在我们解决问题的时候,如果我们能够理解问题,那么我们就距离解决问题不远了!