import com.google.common.base.Predicate;
import com.google.common.base.Function;
import com.google.common.base.Optional;
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// .apis(RequestHandlerSelectors.basePackage("com.crcc.subaccess.service.controller"))
.apis(basePackage("com.crcc.subaccess.service.controller;com.crcc.subaccess.common.controller"))
//.apis(RequestHandlerSelectors.basePackage("com.crcc.subaccess.common.controller"))
.paths(PathSelectors.any())
.build().globalOperationParameters(this.getParameterList());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("后台服务系统")
.description("后台公共服务模块")
.contact("macro")
.version("1.0")
.build();
}
//添加header
private List<Parameter> getParameterList(){
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string"))
.parameterType("header").required(false).build();
pars.add(tokenPar.build());
return pars;
}
//添加多个扫描包
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(";")) {
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());
}