使用 springfox Swagger 自定义文档的端点 [英] Customize endpoints of dockets with springfox Swagger
问题描述
我在互联网上搜索了如何自定义多个文档的端点,但没有找到答案.
I've searched on the internet how to customize endpoints of my multiple dockets, but haven't found the answer.
我的模块有几个 API.我想在不同的端点上生成 Swagger 文档,每个端点都位于其相应 API 的根上.例如:
My module has several APIs. I want to generate Swagger documentation on different endpoints, each one positioned on the root of its corresponding API. For example :
本地主机:8080/v1/subscriptions/doc
localhost:8080/v1/subscriptions/doc
本地主机:8080/v1/buckets/doc
localhost:8080/v1/buckets/doc
我发现只有一种方法可以为我的文档提供不同的端点,但 URL 与我想要的不符.他们是:
I've found only one way to have different endpoints for my dockets, but the URL don't correspond to what I want. They are :
localhost:8080/doc?group=subscriptions
localhost:8080/doc?group=subscriptions
localhost:8080/doc?group=buckets
localhost:8080/doc?group=buckets
这是我的 Swagger 配置文件
Here is my Swagger configuration file
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Value("${info.version}")
private String version;
@Bean
public Docket subscriptionsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("subscriptions")
.apiInfo(subscriptionsApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mymodule"))
.paths(PathSelectors.ant("/v1/subscriptions/**"))
.build();
}
@Bean
public Docket bucketsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("buckets")
.apiInfo(bucketsApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mymodule"))
.paths(PathSelectors.ant("/v1/buckets/**"))
.build();
}
private ApiInfo subscriptionsApiInfo() {
return new ApiInfoBuilder()
.title("Subscriptions Api definition")
.description("Subscriptions Api definition")
.version(version)
.build();
}
private ApiInfo bucketsApiInfo() {
return new ApiInfoBuilder()
.title("Bucket Api definition")
.description("Bucket Api definition")
.version(version)
.build();
}
}
在我的 application.yml 文件中,我写了:
And in my application.yml file, I've written :
springfox.documentation.swagger.v2.path: "/doc"
你知道一种按照我想要的方式定义端点的方法吗?
Do you know a way to define the endpoints on the way I want?
提前致谢
推荐答案
我找到了答案!
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/v1/subscriptions/doc", "/doc?group=subscriptions");
}
}
这篇关于使用 springfox Swagger 自定义文档的端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!