Java 在 Swagger-UI 中对 API 方法进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24951268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:56:17  来源:igfitidea点击:

Sort API methods in Swagger-UI

javajax-rsswaggerswagger-ui

提问by ulrich

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.

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

So far, all methods are displayed in a random order, even not in the order given my source code.

到目前为止,所有方法都以随机顺序显示,甚至不是按照给定我的源代码的顺序。

I use Jax-RS + Jersey 1.

我使用 Jax-RS + Jersey 1。

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.

使用 @ApiOperation 的 position 属性进行排序对我来说不是一种选择,因为方法太多并且 API 仍在扩展,所以如果有新的,我需要更新所有。

Any hints?

任何提示?

采纳答案by Anthony Neace

Update for Swagger UI 2.1.0+:The sorterparameter has been split into two parameters, as noted in Fix 1040, Fix 1280:

Swagger UI 2.1.0+ 的更新:sorter参数已拆分为两个参数,如Fix 1040Fix 1280 中所述

apisSorter

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

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.

api排序器

对 API/标签列表应用排序。它可以是“alpha”(按名称排序)或函数(请参阅 Array.prototype.sort() 以了解排序函数的工作原理)。默认是服务器返回的顺序不变。

操作排序器

对每个 API 的操作列表应用排序。它可以是“alpha”(按字母数字路径排序)、“method”(按 HTTP 方法排序)或函数(请参阅 Array.prototype.sort() 以了解排序函数的工作原理)。默认是服务器返回的顺序不变。

So you'll want to update sorterto apisSorterto sort the API list alphabetically, and/or operationsSorterto sort the operations list of each API. The pet shop demo has updated to apisSorter, as shown below:

因此,您需要更新sorterapisSorter按字母顺序对 API 列表进行排序,和/或operationsSorter对每个 API 的操作列表进行排序。宠物店demo更新为apisSorter,如下图:

Example: (working demo, sorted alphabetically)

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

window.swaggerUi = new SwaggerUi({

...

apisSorter : "alpha"
});

For Swagger UI versions older than 2.1.0:

对于早于 2.1.0 的 Swagger UI 版本

The sorterparameter is still relevant for older versions of Swagger UI:

sorter参数仍然与旧版本的 Swagger UI 相关:

You can use the sorterparameter when instantiating SwaggerUi. This happens in the javascript on the Swagger-Ui index.html. From the documentation:

您可以使用分拣机实例SwaggerUi时参数。这发生在 Swagger-Ui index.html 上的 javascript 中。从文档

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.

sorter 对 API 列表应用排序。它可以是“ alpha”(按字母数字排序路径)或“ method”(按 HTTP 方法排序操作)。默认是服务器返回的顺序不变。

Example:

示例

window.swaggerUi = new SwaggerUi({

...

sorter : "alpha"
});

回答by Kenyakorn Ketsombut

The accepted answer is a bit outdated. In newer versions it is done by:

接受的答案有点过时了。在较新的版本中,它是通过以下方式完成的:

window.swaggerUi = new SwaggerUi({

...

apisSorter: "alpha", // can also be a function
operationsSorter : "method", // can also be 'alpha' or a function
});

回答by Hiddenben

// I had the same issue and i fixed like this 
window.swaggerUi = new SwaggerUi({
    apisSorter: "alpha", 
    operationsSorter: function (a, b) { 
    var order = { 'get': '0', 'post': '1', 'put': '2', 'delete': '3' }; 
    return order[a.method].localeCompare(order[b.method]);    
  },
});

回答by Volodya Lombrozo

Update for Swagger 3.18.3

Swagger 3.18.3 的更新

 window.ui = SwaggerUIBundle({
           ...
            operationsSorter: function (a, b) {
                var order = {'get': '0', 'post': '1', 'put': '2', 'delete': '3'};
                return order[a.get("method")].localeCompare(order[b.get("method")]);
            },
           ...
 });