Swagger UI:如何自定义排序资源
Swagger UI: How to custom sort resources
Quiet 非常直截了当的问题:如何在 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.
中实现一个 "workaround"
在 render 函数中,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 显示订购的标签。
字母顺序似乎是默认的。但是我需要我的标签的自定义顺序。
我今天遇到了这个问题,但由于运气好,我发现标签 ctor 接受了一个 int 命令。
它似乎对我有用!
因此,重新使用您的代码示例:
...
.tags(
new Tag("foo", "tag description", 2),
new Tag("bar", "another desc ", 1),
...
标签 foo
出现在标签 bar
之后 swagger UI。
(我使用的是 springfox v2.9.2)
Quiet 非常直截了当的问题:如何在 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.
中实现一个 "workaround"在 render 函数中,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 显示订购的标签。
字母顺序似乎是默认的。但是我需要我的标签的自定义顺序。
我今天遇到了这个问题,但由于运气好,我发现标签 ctor 接受了一个 int 命令。 它似乎对我有用!
因此,重新使用您的代码示例:
...
.tags(
new Tag("foo", "tag description", 2),
new Tag("bar", "another desc ", 1),
...
标签 foo
出现在标签 bar
之后 swagger UI。
(我使用的是 springfox v2.9.2)