Emi*_*lio 5 rest spring-mvc swagger swagger-ui
安静而直接的问题:如何在 v2.2.6 中对 swagger-ui 中的端点进行排序?我使用 springfox 作为 java 部分。
在我的 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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是 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"
}
]
Run Code Online (Sandbox Code Playgroud)
深入研究问题后,我发现新版本的 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;
})
Run Code Online (Sandbox Code Playgroud)
使用此代码,UI 将显示订购的标签。
| 归档时间: |
|
| 查看次数: |
18052 次 |
| 最近记录: |