我有一个 REST API 和 springfox swagger v2.6.1 并正在运行。但现在,我不想总是显示我拥有的所有控制器,因为其中一些控制器技术性很强,不适合普通用户,但我希望能够选择我显示的内容,而无需重新编译代码。页面顶部有一个下拉字段,显示“默认 (/v2/api-docs)”(或您配置的任何内容),只有这一条目。我的预感是应该可以有多个选项,并根据选项显示或不显示某些控制器类。
由于我不太懂如何上传图片,所以无法提供截图。无论如何,我希望我的问题是清楚的。
在我的项目中进行招摇的代码是最简单的:
@Bean
public Docket api() {
return new Docket( DocumentationType.SWAGGER_2 )
.select()
.apis( RequestHandlerSelectors.any() )
.paths( PathSelectors.any() )
.build()
.apiInfo( metadata() );
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title( "My awesome ACS API" )
.description( "All the requests that the server will respond to." )
.version( "1.0.0" )
.build();
}
我尝试了几种方法,例如添加一些属性、执行两个 .select() 以及选择不同的内容,但我似乎并没有真正接近我希望实现的目标。
谢谢你的帮助!
我能想到的一些选择
您可以使用 SpringSecurity 将身份验证添加到不同的端点,并使端点根本不可访问(但在 Swagger UI 中可见)。
-
您在顶部提到的下拉菜单可以配置如下
@Bean
public Docket orderApi() {
// This will group the endpoints strting with /order.
// And it will be visible as a separate group in the drop down(In Swagger UI)
// with the name 'order' as specified in the groupName(..)
return new Docket(DocumentationType.SWAGGER_2)
.groupName("order")
.apiInfo(metadata())
.select()
.paths(PathSelectors.ant("/order/**"))
.build();
}
@Bean
public Docket orderValidationApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("product")
.apiInfo(metadata())
.select()
.paths(PathSelectors.ant("/product/**"))
.build();
}
-
您可以在 Docker 配置中使用类似这样的内容来完全排除端点在 Swagger UI 中的可见性
return new Docket( DocumentationType.SWAGGER_2 )
.select()
.apis( RequestHandlerSelectors.any() )
.paths(PathSelectors.regex("(?!/error).+")).paths(PathSelectors.regex("(?!/product).+"))
.build()
.apiInfo( metadata() );
这将使 /error 和 /product 以外的所有端点可用。您可以像这样过滤掉端点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)