如何从spring-data-rest entityController中排除OPTIONS,HEAD和PATCH方法

Ach*_*ius 5 spring spring-data-rest springfox

我使用spring-data-rest-webmvc:jar:2.5.3.RELEASE编写REST API并在springfox-data-rest-2.6.0中呈现资源.招摇使得所有方法都存在RestEntityController.我通过使用排除了存储库级别中的一些方法@RestResource(exported = false).但swagger加载所有其他HTTP方法,如OPTIONS,HEAD和PATCH,我无法排除RepositoryRestResource.

如何使用仅包含我的CRUD和搜索方法的干净资源来渲染我的招摇?需要在使用spring-data-rest配置或使用springfox配置时支持此功能.

mik*_*ika 5

在深入研究springfox文档并丢弃一些死胡同之后,我想出了两个可能解决您问题的解决方案.根据您的需要涂抹并混合.

提示#1 - 使用过滤 RequestHandlerSelectors

告诉springfox你想在swagger-ui中显示哪些方法端点.因此,您可以设置RequestHandlerSelector扫描整个应用程序,特定包或已使用共享注释注释的类和方法.

//Example for the method scan based on springfox's @ApiOperation annotation

@Bean
public Docket api() {    
    return new Docket(DocumentationType.SWAGGER_2)          
      .select()                                       
      .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
      .build();
}



// Within your dao or repository class
public interface QueuedMessageDao extends CrudRepository<QueuedMessage, Integer> {
    @ApiOperation(value = "This method finds all messages that are queued with the recipient's email address")
    List<QueuedMessage> findByRecipientEmail(String email); 
}
Run Code Online (Sandbox Code Playgroud)

这将从你的招摇文档中切断大量的弹簧数据休息开销.但是,你仍然会(令人惊讶)看到你的招摇的UI不必要的HTTP方法,比如GET,HEADOPTIONS你的技术方法/<entity>/search端点.我说技术,因为这些端点仅用于发现其他端点,它们不会为您提供来自实体的数据.

提示#2 - 使用过滤 PathSelectors

如果您想要摆脱/<entity>/searchswagger 中的技术端点,可以使用正则表达式排除它们,过滤掉所有搜索端点但保留与您相关的端点.要在实体中仍然拥有搜索的业务端点,您只需要设置隐式GET端点.

//Example for the method scan based on springfox's @ApiOperation annotation

@Bean
public Docket api() {    
    return new Docket(DocumentationType.SWAGGER_2)          
      .select()                                       
      .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.regex("^(?!(/.*(/search)$)).*$"))
      .build();
}

// Within your dao or repository class
public interface QueuedMessageDao extends CrudRepository<QueuedMessage, Integer> {

    @ApiOperation(value = "this generates a business search endpoint")
    List<QueuedMessage> findById(int id);

    @ApiOperation(value = "This method finds all messages that are queued with the recipient's email address")
    List<QueuedMessage> findByRecipientEmail(String email); 
}
Run Code Online (Sandbox Code Playgroud)

现在你摆脱了搜索端点,但仍然在你的招摇文档中进行业务搜索.希望这可以帮助!