这里直接贴代码了哦,看不懂的到我的上篇文章查看哦Swagger2使用教程详解(包含注解)。
提示:网上有种declaringClass的使用方法,该方法在后面已经被弃用,请使用我写的方法就好啦!
···
//定义分割符号
private static final String SPLITOR = ";";
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
log.info(""+swaggerEnabled);
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
//这里用;隔开写多个就可以喽
.apis(RequestHandlerSelectors.basePackage("路径;路径;路径")
.paths(PathSelectors.any())
.build();
}
···
/**
* 切割扫描的包生成Predicate<RequestHandler>
* @param basePackage
* @return
*/
public static Predicate<RequestHandler> scanBasePackage(final String basePackage) {
if(StringUtils.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(StringUtils.isNotBlank(strBasePackage)){
Predicate<RequestHandler> secPredicate = RequestHandlerSelectors.basePackage(strBasePackage);
predicate = predicate == null ? secPredicate : Predicates.or(secPredicate,predicate);
}
}
if(predicate == null)
throw new NullPointerException("basePackage配置不正确,多个包扫描使用"+SPLITOR+"分隔");
return predicate;
}
···
}