我正在使用 Docket 来配置我的 Swagger 2 实例。
但我目前看到的唯一选项是按类型(POST、GET 等)或端点名称(a-z)排序。
我的端点有一个逻辑顺序,我想按这个顺序显示它们
我想要的是:
POST /start
POST /uplaod
POST /finalize
POST /checkStatus
相反,我得到了这样的东西:
POST /checkStatus
POST /finalize
POST /start
POST /upload
代码:
@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()));
}
最佳答案
为 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;
}
关于java - 如何手动对 Swagger UI 上显示的端点进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57528402/