2023-02-16 15:28:15 +08:00
|
|
|
package com.zhgd.config;
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Function;
|
|
|
|
|
import com.google.common.base.Optional;
|
|
|
|
|
import com.google.common.base.Predicate;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
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.service.ApiInfo;
|
|
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @program: Iot
|
|
|
|
|
* @description: Swagger配置类
|
|
|
|
|
* @author: Mr.Wang
|
|
|
|
|
* @create: 2019-04-16 14:46
|
|
|
|
|
**/
|
|
|
|
|
@Configuration
|
|
|
|
|
@EnableSwagger2
|
|
|
|
|
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
|
|
|
|
|
public class SwaggerConfig {
|
|
|
|
|
// 定义分隔符
|
|
|
|
|
private static final String splitor = ";";
|
|
|
|
|
|
|
|
|
|
@Bean
|
2023-08-08 18:20:42 +08:00
|
|
|
public Docket createZwRestApi() {
|
2023-02-16 15:28:15 +08:00
|
|
|
return new Docket(DocumentationType.SWAGGER_2)
|
2023-08-08 18:20:42 +08:00
|
|
|
.groupName("政务接口")
|
|
|
|
|
.apiInfo(apiInfo("政务管理API", "政务前后端联调api文档", "1.0"))
|
2023-02-16 15:28:15 +08:00
|
|
|
.select()
|
|
|
|
|
//.apis(RequestHandlerSelectors.basePackage("com.zhgd.xmgl"))
|
2023-08-08 18:20:42 +08:00
|
|
|
.apis(basePackage("com.zhwl.zw"))
|
2023-02-16 15:28:15 +08:00
|
|
|
.paths(PathSelectors.any())
|
2023-08-08 18:20:42 +08:00
|
|
|
//.paths(PathSelectors.regex("/zw.*"))
|
2023-02-16 15:28:15 +08:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
2023-08-08 18:20:42 +08:00
|
|
|
public Docket createRestApi() {
|
2023-02-16 15:28:15 +08:00
|
|
|
return new Docket(DocumentationType.SWAGGER_2)
|
2023-08-08 18:20:42 +08:00
|
|
|
.groupName("项目接口")
|
|
|
|
|
.apiInfo(apiInfo("项目管理API", "项目前后端联调api文档", "1.0"))
|
2023-02-16 15:28:15 +08:00
|
|
|
.select()
|
|
|
|
|
//.apis(RequestHandlerSelectors.basePackage("com.zhgd.xmgl"))
|
2023-08-08 18:20:42 +08:00
|
|
|
.apis(basePackage("com.zhgd.xmgl"))
|
|
|
|
|
//.apis(basePackage("com.zhgd.xmgl"+splitor+"com.zhgd.xmgl.device"))
|
2023-02-16 15:28:15 +08:00
|
|
|
.paths(PathSelectors.any())
|
2023-08-08 18:20:42 +08:00
|
|
|
|
|
|
|
|
//.paths(PathSelectors.regex("/xmgl.*"))
|
2023-02-16 15:28:15 +08:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-08 18:20:42 +08:00
|
|
|
private ApiInfo apiInfo(String titleName, String description, String version) {
|
2023-02-16 15:28:15 +08:00
|
|
|
return new ApiInfoBuilder()
|
|
|
|
|
.title(titleName)
|
|
|
|
|
.description(description)
|
|
|
|
|
.version(version)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Predicate<RequestHandler> basePackage(final String basePackage) {
|
|
|
|
|
return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
|
|
|
|
|
}
|
2023-08-08 18:20:42 +08:00
|
|
|
|
|
|
|
|
private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
|
2023-02-16 15:28:15 +08:00
|
|
|
return input -> {
|
|
|
|
|
// 循环判断匹配
|
|
|
|
|
for (String strPackage : basePackage.split(splitor)) {
|
|
|
|
|
boolean isMatch = input.getPackage().getName().startsWith(strPackage);
|
|
|
|
|
if (isMatch) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-08-08 18:20:42 +08:00
|
|
|
|
2023-02-16 15:28:15 +08:00
|
|
|
private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
|
|
|
|
|
return Optional.fromNullable(input.declaringClass());
|
|
|
|
|
}
|
|
|
|
|
}
|