package com.javaweb.app.common.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.javaweb.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;
|
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的接口配置
|
*
|
* @author ruoyi
|
*/
|
@Configuration
|
@EnableSwagger2
|
public class SwaggerConfig
|
{
|
/**
|
* 创建API
|
*/
|
@Bean
|
public Docket createRestApi()
|
{
|
return new Docket(DocumentationType.SWAGGER_2)
|
// 详细定制
|
.apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
// 指定当前包路径
|
// .apis(RequestHandlerSelectors.basePackage("com.javaweb.web.controller.tool"))
|
// .apis(basePackage("com.javaweb.web.controller.system,com.javaweb.web.controller.tool"))
|
// 扫描所有 .apis(RequestHandlerSelectors.any())
|
.paths(PathSelectors.any()).build();
|
}
|
|
/**
|
* 添加摘要信息
|
*/
|
private ApiInfo apiInfo()
|
{
|
// 用ApiInfoBuilder进行定制
|
return new ApiInfoBuilder().title("标题:系统_接口文档").description("描述:.....")
|
.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());
|
}
|
}
|