支持多包扫描和注解扫描api

This commit is contained in:
zhangmrit 2018-11-16 18:24:24 +08:00
parent 94e59f72de
commit fb87c428c9
1 changed files with 68 additions and 11 deletions

View File

@ -2,7 +2,14 @@ package com.ruoyi.web.core.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.ruoyi.common.config.Global;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
@ -29,13 +36,12 @@ public class SwaggerConfig
{
return new Docket(DocumentationType.SWAGGER_2)
// 详细定制
.apiInfo(apiInfo())
.select()
.apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 指定当前包路径
.apis(RequestHandlerSelectors.basePackage("com.ruoyi.web.controller.tool"))
// .apis(RequestHandlerSelectors.basePackage("com.ruoyi.web.controller.tool"))
// .apis(basePackage("com.ruoyi.web.controller.system,com.ruoyi.web.controller.tool"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
.paths(PathSelectors.any()).build();
}
/**
@ -44,11 +50,62 @@ public class SwaggerConfig
private ApiInfo apiInfo()
{
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder()
.title("标题若依管理系统_接口文档")
.description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
.contact(new Contact(Global.getName(), null, null))
.version("版本号:" + Global.getVersion())
.build();
return new ApiInfoBuilder().title("标题若依管理系统_接口文档").description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
.contact(new Contact(Global.getName(), null, null)).version("版本号:" + Global.getVersion()).build();
}
/**
* Predicate that matches RequestHandler with given base package name for the class of the handler method.
* This predicate includes all request handlers matching the provided basePackage
*
* @param basePackage - base package of the classes
* @return this
*/
public static Predicate<RequestHandler> basePackage(final String basePackage)
{
return new Predicate<RequestHandler>()
{
@Override
public boolean apply(RequestHandler input)
{
return declaringClass(input).transform(handlerPackage(basePackage)).or(true);
}
};
}
/**
* 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
*
* @param basePackage 扫描包路径
* @return Function
*/
private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)
{
return new Function<Class<?>, Boolean>()
{
@Override
public Boolean apply(Class<?> input)
{
for (String strPackage : basePackage.split(","))
{
boolean isMatch = input.getPackage().getName().startsWith(strPackage);
if (isMatch)
{
return true;
}
}
return false;
}
};
}
/**
* @param input RequestHandler
* @return Optional
*/
@SuppressWarnings("deprecation")
private static Optional<? extends Class<?>> declaringClass(RequestHandler input)
{
return Optional.fromNullable(input.declaringClass());
}
}