美文网首页
Feign传递header

Feign传递header

作者: Alen_ab56 | 来源:发表于2022-11-02 10:09 被阅读0次

    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 {

        }

    相关文章

      网友评论

          本文标题:Feign传递header

          本文链接:https://www.haomeiwen.com/subject/ybzjtdtx.html