fix(FeignConfig.class): feign拦截器添加RequestAttributes是否为空的判断,MQ监听调用时为null

This commit is contained in:
郝先瑞 2022-04-05 22:16:38 +08:00
parent c7bb7d206f
commit 996fd7b328

View File

@ -5,6 +5,7 @@ import feign.RequestInterceptor;
import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
@ -42,17 +43,19 @@ public class FeignConfig {
@Bean @Bean
public RequestInterceptor requestInterceptor() { public RequestInterceptor requestInterceptor() {
return (template) -> { return (template) -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
.getRequestAttributes(); if (requestAttributes != null) {
HttpServletRequest request = attributes.getRequest(); ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
//获取请求头 HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames(); //获取请求头
if (headerNames != null) { Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) { if (headerNames != null) {
String name = headerNames.nextElement(); while (headerNames.hasMoreElements()) {
String values = request.getHeader(name); String name = headerNames.nextElement();
//将请求头保存到模板中 String values = request.getHeader(name);
template.header(name, values); //将请求头保存到模板中
template.header(name, values);
}
} }
} }
}; };