From e85ca910c04d93bc6bc726f2349c5571efab9f43 Mon Sep 17 00:00:00 2001 From: lishangbu Date: Wed, 25 Jul 2018 14:23:34 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=89=B9=E6=80=A7:Swagger=E8=B7=AF=E5=BE=84=E6=8E=92=E9=99=A4?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/SwaggerAutoConfiguration.java | 37 ++++++++++++++++++- .../swagger/config/SwaggerProperties.java | 26 +++++++++++++ .../main/resources/config/pigx-upms-dev.yml | 3 ++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java diff --git a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java index 023318331..b9c27ee31 100644 --- a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java +++ b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java @@ -17,7 +17,10 @@ package com.pig4cloud.pigx.common.swagger.config; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; @@ -43,9 +46,39 @@ import java.util.List; public class SwaggerAutoConfiguration { @Bean - public Docket api() { + @ConditionalOnMissingBean + public SwaggerProperties swaggerProperties() { + return new SwaggerProperties(); + } + + private static final String DEFAULT_EXCLUDE_PATH = "/error"; + private static final String BASE_PATH = "/**"; + + + @Bean + public Docket api(SwaggerProperties swaggerProperties) { + // base-path处理 + // 当没有配置任何path的时候,解析/** + if (swaggerProperties.getBasePath().isEmpty()) { + swaggerProperties.getBasePath().add(BASE_PATH); + } + List> basePath = new ArrayList(); + for (String path : swaggerProperties.getBasePath()) { + basePath.add(PathSelectors.ant(path)); + } + // exclude-path处理 + // 当没有任何配置的时候,解析Spring Boot默认的异常处理路径/error + if(swaggerProperties.getExcludePath().isEmpty()){ + swaggerProperties.getExcludePath().add(DEFAULT_EXCLUDE_PATH); + } + List> excludePath = new ArrayList<>(); + for (String path : swaggerProperties.getExcludePath()) { + excludePath.add(PathSelectors.ant(path)); + } return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() - .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()) + .apis(RequestHandlerSelectors.any()).paths(Predicates.and( + Predicates.not(Predicates.or(excludePath)), + Predicates.or(basePath))) .build().securitySchemes(Collections.singletonList(securitySchema())) .securityContexts(Collections.singletonList(securityContext())).pathMapping("/"); } diff --git a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java new file mode 100644 index 000000000..51994ea3d --- /dev/null +++ b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java @@ -0,0 +1,26 @@ +package com.pig4cloud.pigx.common.swagger.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.ArrayList; +import java.util.List; + +/** + * SwaggerProperties + * + * @author: lisb + * @date: 2018/7/25 14:00 + */ +@Data +@ConfigurationProperties("swagger") +public class SwaggerProperties { + /** + * swagger会解析的url规则 + **/ + private List basePath = new ArrayList<>(); + /** + * 在basePath基础上需要排除的url规则 + **/ + private List excludePath = new ArrayList<>(); +} diff --git a/pigx-config/src/main/resources/config/pigx-upms-dev.yml b/pigx-config/src/main/resources/config/pigx-upms-dev.yml index 7a584e032..1ff1f4723 100644 --- a/pigx-config/src/main/resources/config/pigx-upms-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-upms-dev.yml @@ -38,3 +38,6 @@ mybatis-plus: configuration: map-underscore-to-camel-case: true cache-enabled: true +# swagger相关配置 +swagger: + exclude-path: /actuator/**,/error