openfeign 转发post 接口 错误 Incomplete output stream executing POST http://xxxx
问题描述
当采用feign转发 post请求时,报错
@RequestLine("POST")
ResponseResult<Object> forwardPost(URI uri, @Param Object body);
转发服务定义了拦截
.requestInterceptor(requestTemplate -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (Objects.nonNull(headerNames)) {
while (headerNames.hasMoreElements()) {
String key = headerNames.nextElement();
String values = request.getHeader(key);
requestTemplate.header(key, values);
}
} else {
requestTemplate.header("error", "no header");
}
})
运行就报错
Incomplete output stream executing POST http://localhost:8080/hello/obj"
经过验证发现是 自定义 requestTemplate 添加请求头,导致post类型请求循环调用,报错 百度找不到原因, 很多都在说添加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
配置
feign:
httpclient:
enabled: true
这个解决不了本文问题
最终必应搜到github有解答
headers.remove("content-length");
it work
# 链接
https://github.com/spring-cloud/spring-cloud-openfeign/issues/390
remove the header param content-length can resolve it
解决方案,不要添加"content-length"
if (!"content-length".equals(key)) {
String values = request.getHeader(key);
requestTemplate.header(key, values);
}
|