设置Feign的Header信息
设置Feign的Header信息
两种方式
1.在方法参数前面添加@RequestHeader注解
@PostMapping(value = "/getStuDetail")
public ReturnInfo getStudentDetail(@RequestBody Map map,@RequestHeader(name = "id") String id);
使用@RequestHeader(name = “id”)可以传递动态header属性
2.实现RequestInterceptor接口,设置Header(所有Feign请求)
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import feign.RequestInterceptor;
import feign.RequestTemplate;
@Configuration
public class FeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
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);
template.header(name, values);
}
}
}
}
//configuration = FeignConfiguration.class===================
@Component@FeignClient(value = "xxxxxxxxx",fallback = XxxxxServiceHystric.class ,configuration = FeignConfiguration.class)
public interface XxxxxService {
}
扫码关注公众号 领取精品视频学习资源
相关文章