package com.dimp.mobile.report.feign.config; import feign.RequestInterceptor; import feign.RequestTemplate; import org.springframework.http.HttpHeaders; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; /** * @description 解决feign组件来调用服务之间传递token问题 */ public class FeignConfig implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //仅仅添加token,和下面的逻辑代码二选其一 requestTemplate.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION)); //传递get请求参数到request对象 String userId = (String) request.getAttribute(AppContextConstant.USER_ID); if (StringUtils.isNotBlank(userId)) { requestTemplate.query(AppContextConstant.USER_ID, userId); } // //传递header里面所有内容 // Enumeration<String> headerNames = request.getHeaderNames(); // if (headerNames != null) { // while (headerNames.hasMoreElements()) { // String name = headerNames.nextElement(); // String values = request.getHeader(name); // requestTemplate.header(name, values); // } // } } }
@FeignClient(name = "report", configuration = FeignConfig.class) public interface ReportFeign { @RequestMapping("/report/comment/organizationTree") public ResultDTO getOrganizationOfUsers(); //获取默认组织维度树 }
比如String,Integer....用@RequestParam("参数的别名")注解,括号中一定要有值(参数的别名)。调用方需要加注解,被调用方不需要加,被调用方的参数名和调用方的别名保持一致即可。
调用方不需要加注解,被调用方加上@RequestBody()注解即可.
调用方需要通过@RequestParam("参数的别名")注解指定别名,被调用方需要加上注解@PathVariable()即可,如果被调用方参数名称没有和调用方别名保持一致,需要在PathVariable注解中设置和调用方一致的别名。