Swagger在POM配置好后,修改SwaggerConfig类,我只贴出要修改的,直接用
/**注意导入包是否齐全**/
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());
}
}