我有一个 REST API 和 springfox swagger v2.6.1,并且可以正常工作。但是现在,我不想总是显示我拥有的所有 Controller ,因为其中一些非常技术性,不适合普通用户,但我希望能够选择我显示的内容而无需重新编译代码。页面顶部有一个下拉字段,上面写着“默认(/v2/api-docs)”(或您将其配置为的任何内容),只有一个条目。我的直觉是应该可以有多个选项,并根据选项显示或不显示某些 Controller 类。
由于我不太会上传图片,所以无法提供截图。无论如何,我希望我的问题很清楚。
在我的项目中做 swagger 的代码是最简单的:
@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 的端点可用。您可以像这样过滤掉端点。
关于java - Swagger 过滤 API 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43935041/