标签:man header tostring 解决 ext err app rgb intercept
request的header信息在通过feign调用的时候,不能直接传过去,可以写一个拦截器,给他发过去,但是中间出了个小bug,报错too many bytes written,经百度,找了如下https://blog.csdn.net/qq_39986681/article/details/107138740解决,最后的参考代码如下
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);
if (name.equals("content-length")){
continue;
}
requestTemplate.header(name, values);
}
}
Enumeration<String> bodyNames = request.getParameterNames();
StringBuffer body =new StringBuffer();
if (bodyNames != null) {
while (bodyNames.hasMoreElements()) {
String name = bodyNames.nextElement();
String values = request.getParameter(name);
body.append(name).append("=").append(values).append("&");
}
}
if(body.length()!=0) {
body.deleteCharAt(body.length()-1);
requestTemplate.body(body.toString());
// log.info("feign interceptor body:{}",body.toString());
}
// log.info("feign interceptor method:{}",request.getMethod());
}
}
标签:man header tostring 解决 ext err app rgb intercept
原文地址:https://www.cnblogs.com/yuan416/p/14620944.html