首页 文章

Swagger API操作订购

提问于
浏览
3

如何按字母顺序按方法对操作进行排序,例如: DELETEGETPOSTPUT .

我已经阅读过这篇文章,但它是用HTML格式的,但就我而言,我已将Swagger集成到Spring Boot中,因此我需要在创建Docket时对其进行排序 .

Sort API methods in Swagger UI

然后我在Docket中注意到了这个方法 operationOrdering() ,但我仍然无法使它工作 .

1 回答

  • 3

    我正在使用Springfox版本2.8.0,以下代码片段适用于我记录的API:

    @Bean
    UiConfiguration uiConfig() {
        return UiConfigurationBuilder
                .builder()
                .operationsSorter(OperationsSorter.METHOD)
    
                ...
    
                .build();
    }
    

    有两个可能的值:

    • OperationsSorter.ALPHA - 按 path 按字母顺序对API endpoints 排序

    • OperationsSorter.METHOD - 按 method 按字母顺序对API endpoints 排序

    OperationsSorter.METHOD 正是您要找的 .


    Alternative 使用 operationOrdering()

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
    
            ...
    
            .operationOrdering(new Ordering<Operation>() {
                @Override
                public int compare(Operation left, Operation right) {
                    return left.getMethod().name().compareTo(right.getMethod().name());
                }
            })
    }
    

    但是,这不起作用,因为Springfox中的一个错误似乎仍处于活动状态(Operation ordering is not working) .

相关问题