| package com.doumee.config.swagger; | 
|   | 
| import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; | 
| import com.google.common.base.Function; | 
| import com.google.common.base.Optional; | 
| import org.springframework.beans.factory.annotation.Value; | 
| 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.oas.annotations.EnableOpenApi; | 
| import springfox.documentation.service.ApiInfo; | 
| import springfox.documentation.spi.DocumentationType; | 
| import springfox.documentation.spring.web.plugins.Docket; | 
|   | 
| import java.util.function.Predicate; | 
|   | 
| /** | 
|  * Swagger配置 | 
|  * @author Eva.Caesar Liu | 
|  * @date 2022/03/11 10:24 | 
|  */ | 
| @Configuration | 
| @EnableOpenApi | 
| @EnableKnife4j | 
| public class SwaggerConfig { | 
|   | 
|     @Value("${swagger.host:}") | 
|     private String host; | 
|   | 
|     @Value("${swagger.title:接口文档}") | 
|     private String title; | 
|   | 
|     @Value("${swagger.description:}") | 
|     private String description; | 
|   | 
|     @Value("${project.version:}") | 
|     private String version; | 
|   | 
|     @Bean | 
|     public ApiInfo getApiInfo() { | 
|         return new ApiInfoBuilder() | 
|                 .title(title) | 
|                 .description(description) | 
|                 .version(version) | 
|                 .build(); | 
|     } | 
|     @Bean | 
|     public Docket getDocket() { | 
|         return new Docket(DocumentationType.SWAGGER_2) | 
|                 .apiInfo(this.getApiInfo()).groupName("default") | 
|                 .host(host) | 
|                 .select() | 
|                 .apis( basePackage("com.doumee.cloud")) | 
| //                .apis( basePackage("com.doumee.api.common")) | 
|   | 
| //                .apis( basePackage("com.doumee.cloud.openapi;com.doumee.api.common")) | 
| //                .apis( basePackage("com.doumee.cloud.openapi")) | 
|                 // 设置需要被扫描的类,这里设置为添加了@Api注解的类 | 
| //                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) | 
|                 .paths(PathSelectors.any()) | 
|                 .build(); | 
|     } | 
|     /** | 
|      * 重写basePackage方法,使能够实现多包访问,复制贴上去 | 
|      * @author  teavamc | 
|      * @date 2019/1/26 | 
|      * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler> | 
|      */ | 
|     public static Predicate<RequestHandler> basePackage(String basePackage) { | 
|         return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true); | 
|     } | 
|   | 
|     private static Function<Class<?>, Boolean> handlerPackage(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()); | 
|     } | 
| } |