feign header这样设置无效
@GetMapping("/xxx") @Headers( {"yyy: 1", "888-666: wangyong@xxx.com"}) DataResponsequeryxxx(@RequestParam String zz);
正确设置:
新增配置类
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
@Component
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// 设置token
Map> headers = requestTemplate.headers();
if(MapUtils.isEmpty(headers) || CollectionUtils.isEmpty(headers.get("XXX"))){
requestTemplate.header("xxx", userToken);
}
// 如果配置了 H 的线程模式,这里还会有问题
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(Objects.isNull(attributes)){
return;
}
HttpServletRequest request = attributes.getRequest();
String authHeaderValue = request.getHeader(Constant.HR_GLOBAL_TOKEN_NAME);
if (StringUtils.isNotBlank(authHeaderValue)) {
requestTemplate.header(yyy, authHeaderValue);
}
}
}
并且同样的header重复设置无效,只有第一次设置的可以成功,如
requestTemplate.header(yyy,"111");
再设置
requestTemplate.header(yyy,"222");
那么接收后yyy的值为第一次设置的"111"
可以先移除header
requestTemplate.removeHeader(yyy)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)