码迷,mamicode.com
首页 > 其他好文 > 详细

feign调用之请求头的数据

时间:2021-04-07 11:20:39      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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());
}
}

feign调用之请求头的数据

标签:man   header   tostring   解决   ext   err   app   rgb   intercept   

原文地址:https://www.cnblogs.com/yuan416/p/14620944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!