安静而直接的问题:如何在 v2.2.6 中对 swagger-ui 中的端点进行排序?我在 java 部分使用 springfox。
在我的 swagger 主页中,我有一个按标签分组的资源集合。这些标签的顺序是随机的。我想按自定义顺序(如果不可能,按字母顺序排列)。
我的 SwaggerConfig.java 文件:
package cat.meteo.apiinterna.commons.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.tags(
new Tag("XEMA Me", "1"),
new Tag("XEMA Ul", "2"),
new Tag("XEMA Ag", "3"),
new Tag("Prono", "4"),
new Tag("Sound", "5")
)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API REST")
.description("Self-documented API")
.version("v0.1.0")
.build();
}
}
这是 swagger-ui 以相同顺序使用的标记结果 json 文件。如您所见,顺序似乎是随机的。不是 java 标签顺序,不是按字母顺序。
( http://localhost:8080/XXX/v2/api-docs )
"tags": [
{
"name": "XEMA Ul",
"description": "2"
},
{
"name": "XEMA Me",
"description": "1"
},
{
"name": "XEMA Ag",
"description": "3"
},
{
"name": "Sound",
"description": "5"
},
{
"name": "Prono",
"description": "4"
}
]
最佳答案
深入研究问题后,我发现新版本的 swagger-ui 不支持自定义排序作为选项。 Springfox 也不会给生成的 json 重新排序的机会,因此唯一的方法是在 swagger-ui 中实现“解决方法”。
在渲染函数中,swagger-ui.js 文件(v2.2.6)的第 21766 行:
// Workaround: alphabetically ordered tags
this.api.apisArray.sort(function (a, b) {
if (a.tag < b.tag)
return -1;
if (a.tag > b.tag)
return 1;
return 0;
})
使用此代码,UI 显示已排序的标签。
关于rest - Swagger UI : How to custom sort resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614744/