如何手动排序 Swagger UI 上显示的端点?

use*_*144 8 java java-8 swagger-2.0

我正在使用 Docket 来配置我的 Swagger 2 实例。但我目前看到的唯一选项是按类型(POST、GET 等)或端点名称 (az) 排序。

我的端点有一个逻辑顺序,我想按该顺序显示它们

我想要的是:

POST /start
POST /uplaod
POST /finalize
POST /checkStatus
Run Code Online (Sandbox Code Playgroud)

相反,我得到这样的东西:

POST /checkStatus
POST /finalize
POST /start
POST /upload
Run Code Online (Sandbox Code Playgroud)

代码:

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host(swaggerHost)
                .pathProvider(new RelativePathProvider(servletContext) {

                    @Override
                    public String getApplicationBasePath() {
                        return swaggerBasePath;
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build()
                .apiInfo(apiInfo())
                .securitySchemes(Collections.singletonList(securitySchema()))
                .securityContexts(Collections.singletonList(securityContext()));
    }
Run Code Online (Sandbox Code Playgroud)

Cha*_*esA 0

为 swagger UI 订购端点的一种简单方法是使用标签:https ://swagger.io/docs/specification/grouping-operations-with-tags/

编辑:添加我用来解决以下评论的替代方案:我倾向于将操作排序器与自定义比较函数一起使用。例子:

operationsSorter: function (a, b) {
  var order = {'get': '0', 'post': '1', 'patch': '2', 'put': '3', 'delete': '4'};
  if (a.get('path') == b.get('path')) {
    return order[a.get('method')].localeCompare(order[b.get('method')]);
  }
  if (a.get('path').split('/').length == b.get('path').split('/').length) {
    return a.get('path').split('/').slice(-1)[0].localeCompare(b.get('path').split('/').slice(-1)[0]);
  }
  return a.get('path').split('/').length - b.get('path').split('/').length;
}
Run Code Online (Sandbox Code Playgroud)