网上查找的多包扫描,基本上是
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Function;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger2的接口配置
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
// 定义分隔符,配置Swagger多包
private static final String splitor = ";";
/**
* 创建API
*/
@Bean
public Docket createRestApi()
{
return new Docket(DocumentationType.SWAGGER_2)
// 详细定制
.apiInfo(apiInfo())
.select()
// 指定当前包路径,这里就添加了两个包,注意方法变成了basePackage,中间加上成员变量splitor
.apis(basePackage("com.XX.api.controller.broad"+splitor+"com.XX.api.controller.village"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
/**
* 添加摘要信息
* 这里是接口的描述配置,不重要
*/
private ApiInfo apiInfo()
{
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder()
.title("XXX系统_接口文档")
.description("用于XXX统开发组生成RESTapi风格的接口...")
// .contact(new Contact(Global.getName(), null, null))
// .version("版本号:" + Global.getVersion())
.build();
}
/**
* 重写basePackage方法,使能够实现多包访问,复制贴上去
* @author teavamc
* @date 2019/1/26
* @param [basePackage]
* @return com.google.common.base.Predicate<springfox.documentation.RequestHandler>
*/
public static Predicate<RequestHandler> basePackage(final String basePackage) {
return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
}
private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
return input -> {
// 循环判断匹配
for (String strPackage : basePackage.split(splitor)) {
boolean isMatch = input.getPackage().getName().startsWith(strPackage);
if (isMatch) {
return true;
}
}
return false;
};
}
private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
return Optional.fromNullable(input.declaringClass());
}
}
这里存在一个过时的方法input.declaringClass()
基于此进行修改如下,测试的版本为2.9.2
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger2版本:2.9.2
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private static final String SPLITOR = ";";
/**
* 切割扫描的包生成Predicate<RequestHandler>
* @param basePackage
* @return
*/
public static Predicate<RequestHandler> scanBasePackage(final String basePackage) {
if(StrUtil.isBlank(basePackage)){
throw new NullPointerException("basePackage不能为空,多个包扫描使用"+SPLITOR+"分隔");
}
String[] controllerPack = basePackage.split(SPLITOR);
Predicate<RequestHandler> predicate = null;
for (int i = controllerPack.length -1; i >= 0 ; i--) {
String strBasePackage = controllerPack[i];
if(StrUtil.isNotBlank(strBasePackage)){
Predicate<RequestHandler> tempPredicate = RequestHandlerSelectors.basePackage(strBasePackage);
predicate = predicate == null ? tempPredicate : Predicates.or(tempPredicate,predicate);
}
}
if(predicate == null){
throw new NullPointerException("basePackage配置不正确,多个包扫描使用"+SPLITOR+"分隔");
}
return predicate;
}
/**
* 自定义Docket
* groupName需配置,否则会报java.lang.IllegalStateException: Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket customDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName( "demo-custom")
.apiInfo(apiInfo())
.select()
.apis(scanBasePackage("com.xxx.demo.controller"
+SPLITOR+"com.xxx.test.controller" ))
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("内容:接口文档")
.description("描述:微信公众号开发")
.contact(new Contact("XXX", null, "xxx@qq.com"))
.version("版本号:" + 1.0)
.build();
}
}