1. 通过 @RequestHeader(name = "headerName") 来传递
@FeignClient(name ="service-name")publicinterface XXXFeignClient {
@RequestMapping(value ="/user/getUserTicket", method = RequestMethod.GET)
String getUserTicket(@RequestParam("id") String id,@RequestHeader(name ="Referer") String referer);}
2. 通过实现 RequestInterceptor 接口
@ConfigurationpublicclassFeignConfigurationimplements RequestInterceptor{ @Overridepublicvoid apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration headerNames = request.getHeaderNames();
if(headerNames !=null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
requestTemplate.header(name, values);
}} }}
然后将自定义的配置类配置到Feign中,就可以实现该Feign中接口Header参数的传递
@FeignClient(value ="service-name",configuration = FeignConfiguration.class)
publicinterface XXXFeignClient {
}
网友评论