在Swagger-UI中对API方法进行排序 [英] Sort API methods in Swagger-UI
问题描述
我找不到任何工作示例,如何实现以下目标:
我希望我的Swagger-UI中的API方法按方法排序(GET-POST-PUT-DELETE)或按字母顺序排序。
I cannot find any working example, how to achieve the following: I want my API methods in the Swagger-UI sorted either by methods (GET-POST-PUT-DELETE) OR/AND alphabetically.
到目前为止,所有方法都以随机顺序显示,即使不是按照我的源代码的顺序显示。
So far, all methods are displayed in a random order, even not in the order given my source code.
我使用Jax-RS + Jersey 1.
I use Jax-RS + Jersey 1.
使用@ApiOperation的position属性进行排序对我来说不是一个选项,因为有太多的方法而且API仍然是如果有新的话,我需要更新所有内容。
Sorting using the position attribute of @ApiOperation is not an option for me, as there are too many methods and the API is still extending, so I would need to update all if there is a new one.
任何提示?
推荐答案
Swagger UI 2.1.0 +的更新: 分拣机
参数已分为两个参数,如上所述在修复1040 中,修复1280 :
Update for Swagger UI 2.1.0+: The sorter
parameter has been split into two parameters, as noted in Fix 1040, Fix 1280:
apisSorter
apisSorter
申请到API /标签列表。它可以是'alpha'
(按名称排序)或函数(请参阅Array.prototype.sort()以了解
sort函数的工作原理)。默认值是服务器返回的订单
不变。
Apply a sort to the API/tags list. It can be 'alpha' (sort by name) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
operationsSorter
operationsSorter
将排序应用于每个API的操作列表。它可以是'alpha'(按字母数字路径排序),'method'(按
HTTP方法排序)或函数(请参阅Array.prototype.sort()以了解
sort函数的工作原理)。默认值是服务器返回的订单
不变。
Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
所以你想要更新分拣机
到 apisSorter
按字母顺序排序API列表,和/或 operationsSorter
来排序每个API的操作列表。宠物店演示已更新为apisSorter,如下所示:
So you'll want to update sorter
to apisSorter
to sort the API list alphabetically, and/or operationsSorter
to sort the operations list of each API. The pet shop demo has updated to apisSorter, as shown below:
Example: (working demo, sorted alphabetically)
window.swaggerUi = new SwaggerUi({
...
apisSorter : "alpha"
});
对于早于2.1.0的Swagger UI版本:
分拣机
参数仍与旧版本的Swagger UI相关:
The sorter
parameter is still relevant for older versions of Swagger UI:
在实例化SwaggerUi时,您可以使用 sorter 参数。这发生在Swagger-Ui index.html上的javascript中。来自文档:
You can use the sorter parameter when instantiating SwaggerUi. This happens in the javascript on the Swagger-Ui index.html. From the documentation:
sorter对API列表应用排序。它可以是 alpha (按字母数字排序路径
)或方法(HTTP方法的排序操作)。
默认值是服务器返回的订单不变。
sorter apply a sort to the API list. It can be 'alpha' (sort paths alphanumerically) or 'method' (sort operations by HTTP method). Default is the order returned by the server unchanged.
示例:
window.swaggerUi = new SwaggerUi({
...
sorter : "alpha"
});
这篇关于在Swagger-UI中对API方法进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!