| ¶Ô±ÈÐÂÎļþ | 
|  |  |  | 
|---|
|  |  |  | 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 getDocket2() { | 
|---|
|  |  |  | return new Docket(DocumentationType.SWAGGER_2) | 
|---|
|  |  |  | .apiInfo(this.getApiInfo()).groupName("default") | 
|---|
|  |  |  | .host(host) | 
|---|
|  |  |  | .select() | 
|---|
|  |  |  | .apis( basePackage("com.doumee.api.gateway;")) | 
|---|
|  |  |  | // è®¾ç½®éè¦è¢«æ«æçç±»ï¼è¿é设置为添å äº@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()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|