feign.RetryableException: too many bytes written executing POST
在使用feign的时候 出现这个错误 记录一下原代码如下import feign.RequestInterceptor;import feign.RequestTemplate;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.requ
·
在使用feign的时候 出现这个错误 记录一下
原代码如下
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* ***************************************************
*
* @Auther:
* @Descipion:
* @CreateDate:
* ****************************************************
*/
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
requestTemplate.header(name, values);
}
}
}
}
因为服务之间调用需要携带一些用户信息之类的 所以实现了Feign的RequestInterceptor
拦截器复制请求头,复制的时候是所有头都复制的,可能导致Content-length长度跟body不一致. 所以只需要判断如果是Content-length就跳过
修改后代码如下
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* ***************************************************
*
* @Auther: zianY
* @Descipion: TODO
* @CreateDate: 2021-01-22
* ****************************************************
*/
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
// 跳过 content-length
if (name.equals("content-length")){
continue;
}
requestTemplate.header(name, values);
}
}
}
}
参考文章:
Feign报错feign.RetryableException: too many bytes written executing
更多推荐
所有评论(0)