在 Swagger-UI 中对 API 方法进行排序 [英] Sort API methods in Swagger-UI

查看:47
本文介绍了在 Swagger-UI 中对 API 方法进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何工作示例,如何实现以下目标:我希望 Swagger-UI 中的 API 方法按方法 (GET-POST-PUT-DELETE) 或/AND 按字母顺序排序.

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 的位置属性进行排序对我来说不是一个选项,因为方法太多,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+ 更新: sorter 参数已拆分为两个参数,如 Fix 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:

apisorter

对 API/标签列表应用排序.它可以是阿尔法"(按名称排序)或函数(请参阅 Array.prototype.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.

操作排序器

对每个 API 的操作列表应用排序.它可以是 'alpha'(按字母数字路径排序)、'method'(按HTTP 方法)或函数(参见 Array.prototype.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.

因此,您需要将 sorter 更新为 apisSorter 以按字母顺序对 API 列表进行排序,和/或将 operationsSorter 更新为对操作列表进行排序每个 API 的.宠物店demo更新为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:

示例:(工作演示,按字母排序)

window.swaggerUi = new SwaggerUi({

...

apisSorter : "alpha"
});

对于早于 2.1.0 的 Swagger UI 版本:

sorter 参数仍然适用于旧版本的 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'(排序路径字母数字)或 'method'(按 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆