【问题标题】:FeignClients get published as REST endpoints in spring cloud applicationFeignClients 在 Spring Cloud 应用程序中作为 REST 端点发布
【发布时间】:2017-06-03 01:30:11
【问题描述】:

我的应用程序中定义了 REST FeignClient

@FeignClient(name = "gateway", configuration = FeignAuthConfig.class)
public interface AccountsClient extends Accounts {

}

我在服务器和客户端之间共享端点接口:

@RequestMapping(API_PATH)
public interface Accounts {


    @PostMapping(path = "/register",
            produces = APPLICATION_JSON_VALUE,
            consumes = APPLICATION_JSON_VALUE)
    ResponseEntity<?> registerAccount(@RequestBody ManagedPassUserVM managedUserDTO)
            throws EmailAlreadyInUseException, UsernameAlreadyInUseException, URISyntaxException;

}

除了我在客户端应用程序中的FeignClient 定义也被注册为独立的 REST 端点之外,一切正常。

目前,我尝试使用过滤器来防止这种行为,该过滤器在我的客户端应用程序中返回FeignClinet 客户端映射的404 状态代码。然而,这种工作环境似乎很不雅。

还有其他方法可以防止假装客户端注册为单独的 REST 端点吗?

【问题讨论】:

    标签: spring-mvc spring-cloud spring-cloud-netflix netflix-feign spring-cloud-feign


    【解决方案1】:

    我已经为这个错误的 Spring Framework 行为使用了解决方法:

    @Configuration
    @ConditionalOnClass({Feign.class})
    public class FeignMappingDefaultConfiguration {
        @Bean
        public WebMvcRegistrations feignWebRegistrations() {
            return new WebMvcRegistrationsAdapter() {
                @Override
                public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                    return new FeignFilterRequestMappingHandlerMapping();
                }
            };
        }
    
        private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
            @Override
            protected boolean isHandler(Class<?> beanType) {
                return super.isHandler(beanType) && (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null);
            }
        }
    }
    

    在SpringCloudissue找到的

    【讨论】:

      【解决方案2】:

      这是 Spring Cloud 的 feign 支持的一个已知限制。通过将@RequestMapping 添加到接口,Spring MVC(不是 Spring Cloud)假定您想要作为端点。 Feign 接口上的@RequestMapping 目前不支持。

      【讨论】:

      • 这似乎是 SpringMvc 中的一个错误。我还尝试从组件扫描中省略我的 FeignClients,但它不起作用。
      • 可以肯定的是,它已经成为一个相当长的时间了。这是一个快捷方式,因此您不必同时输入@RequestMapping@Component
      • 似乎他们忘记在文档或 javadoc 中提及此行为。这似乎也是非常出乎意料的行为。
      猜你喜欢
      • 2018-08-11
      • 2019-08-26
      • 2015-08-13
      • 2018-02-10
      • 2020-02-16
      • 2019-05-10
      • 2017-02-10
      • 1970-01-01
      • 2016-10-10
      相关资源
      最近更新 更多