【问题标题】:Swagger API Operations OrderingSwagger API 操作排序
【发布时间】:2017-06-07 16:04:42
【问题描述】:

如何按字母顺序对我的操作进行排序,例如DELETEGETPOSTPUT

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

Sort API methods in Swagger UI

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

【问题讨论】:

    标签: java spring-boot swagger springfox


    【解决方案1】:

    我使用的是 Springfox 2.8.0 版,以下代码 sn-p 适用于我记录的 API:

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

    有 2 个可能的值:

    • OperationsSorter.ALPHA - 按路径的字母顺序对 API 端点进行排序
    • OperationsSorter.METHOD - 按方法
    • 的字母顺序对 API 端点进行排序

    OperationsSorter.METHOD 就是你要找的东西。


    替代使用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)。

    【讨论】:

    • @DanielHári 你是指OperationsSorter 还是替代方案?
    • 它们都不起作用,它按url对所有操作进行排序。
    • @DanielHári 我在我的项目中使用 Springfox 2.9.2 版,OperationSorter 方法适用于我。 ALPHAMETHOD。无法复制。
    【解决方案2】:
    @Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder
                .builder()
                .operationsSorter(OperationsSorter.METHOD)
                .build();
    }
    

    这对我有用。我正在使用 Spring Boot 2.2.0.M6,Swagger UI 2.9.2

    【讨论】:

      【解决方案3】:

      对于 Spring Boot 2.4OpenAPIapplication.properties 中的以下属性可能会令人感兴趣:

      • springdoc.swagger-ui.tagsSorter=alpha
      • springdoc.swagger-ui.operations-sorter=alpha

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 2019-03-04
        • 2014-09-17
        • 1970-01-01
        • 1970-01-01
        • 2019-07-23
        • 2013-11-17
        相关资源
        最近更新 更多