swagger的API扫描提供了四种方式,分别如下:
- 1、RequestHandlerSelectors.any() 匹配任何controller的接口
- 2、RequestHandlerSelectors.withClassAnnotation() 扫描含有类注解的
- 3、RequestHandlerSelectors.withMethodAnnotation() 扫描含有方法注解的
- 3、RequestHandlerSelectors.basePackage() 扫描指定包路径
@Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.fan.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.any()) .build().globalOperationParameters(defaultHeader()); }
如上代码,在.apis()方法中可以进行配置扫描条件,可以通过apis进行多次设置,apis方法参数为Predicate<RequestHandler> selector。
// apis方法源码 public ApiSelectorBuilder apis(Predicate<RequestHandler> selector) { this.requestHandlerSelector = Predicates.and(this.requestHandlerSelector, selector); return this; }
查看apis方法的源码可以发现,条件参数selector被Predicates以and()方法操作,并返回一个Predicate<RequestHandler>类,Predicates.and()实际上完成"条件1 and 条件2"的操作,类似于sql语句where条件的内容,根据这个特点,可以通过Predicates.and()和Predicates.or()的方法完成"(条件1 and 条件2) or 条件3"的操作,就可以扫描任何指定包下的API,并实现多路径扫描、交集、并集等配置。swagger示例如下:
package com.fan.ams.config; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; @EnableWebMvc @EnableSwagger2 @Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(getBasePackages()) .paths(PathSelectors.any()) .build(); } // 设置多路径 private Predicate<RequestHandler> getBasePackages() { Predicate<RequestHandler> predicate = Predicates.and(RequestHandlerSelectors.basePackage("com.fan.ams.controller")); // 扫描com.fan.ams.controller包 predicate = Predicates.and(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class), predicate) // 扫描com.fan.ams.controller包下有@ApiOperation,和上面是交集 predicate = Predicates.or(RequestHandlerSelectors.basePackage("com.fan.ams.controller222"), predicate)// 扫描com.fan.ams.controller222包,和上面是并集 return predicate; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("后台API接口文档") .description("后台API接口文档") .termsOfServiceUrl("") .contact(new Contact("", "", "1663501@qq.com")) .version("V 1.0.0") .build(); } }