From 9e881598f5d45252cbf35a764cc0ec98bdc0ecdf Mon Sep 17 00:00:00 2001 From: lbw Date: Wed, 6 Sep 2023 14:20:02 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=20knife4j=20=E6=9C=8D=E5=8A=A1=E5=8F=91=E7=8E=B0?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pig-common/pig-common-bom/pom.xml | 6 ++ pig-gateway/pom.xml | 5 ++ .../config/SpringDocConfiguration.java | 67 +++++++++++++++++-- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/pig-common/pig-common-bom/pom.xml b/pig-common/pig-common-bom/pom.xml index d95032b4..6bea1680 100644 --- a/pig-common/pig-common-bom/pom.xml +++ b/pig-common/pig-common-bom/pom.xml @@ -23,6 +23,7 @@ 4.9.9 0.0.39 1.2.83 + 3.0.3 2.1.0 2.2.14 3.5.3.2 @@ -103,6 +104,11 @@ ${mysql.version} + + io.springboot + knife4j-openapi3-ui + ${knife4j.version} + org.springdoc springdoc-openapi-starter-webflux-ui diff --git a/pig-gateway/pom.xml b/pig-gateway/pom.xml index 6bb81ef8..6055f423 100755 --- a/pig-gateway/pom.xml +++ b/pig-gateway/pom.xml @@ -83,6 +83,11 @@ org.springdoc springdoc-openapi-starter-webflux-ui + + + io.springboot + knife4j-openapi3-ui + diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SpringDocConfiguration.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SpringDocConfiguration.java index fbc48c4f..cb5c1989 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SpringDocConfiguration.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/SpringDocConfiguration.java @@ -8,21 +8,31 @@ import com.alibaba.nacos.common.utils.StringUtils; import lombok.RequiredArgsConstructor; import org.springdoc.core.properties.AbstractSwaggerUiConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigProperties; +import org.springdoc.webflux.ui.SwaggerResourceResolver; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.core.io.Resource; +import org.springframework.web.reactive.resource.ResourceResolverChain; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; /** - * SpringDoc配置类,实现InitializingBean接口 swagger 3.0 展示 - * + * @author lengleng + * @date 2022/3/26 + *

+ * swagger 3.0 展示 */ @RequiredArgsConstructor @Configuration(proxyBeanMethods = false) -@ConditionalOnProperty(value = "springdoc.api-docs.enabled", matchIfMissing = true) +@ConditionalOnProperty(value = "springdoc.api-docs.enabled",matchIfMissing = true) public class SpringDocConfiguration implements InitializingBean { private final SwaggerUiConfigProperties swaggerUiConfigProperties; @@ -34,7 +44,56 @@ public class SpringDocConfiguration implements InitializingBean { */ @Override public void afterPropertiesSet() { - NotifyCenter.registerSubscriber(new SwaggerDocRegister(swaggerUiConfigProperties, discoveryClient)); + SwaggerDocRegister swaggerDocRegister = new SwaggerDocRegister(swaggerUiConfigProperties, discoveryClient); + // 手动调用一次,避免监听事件掉线问题 + swaggerDocRegister.onEvent(null); + NotifyCenter.registerSubscriber(swaggerDocRegister); + } + + /** + * Swagger resource resolver swagger resource resolver. + * @param swaggerUiConfigProperties the swagger ui config properties + * @return the swagger resource resolver + */ + @Bean + @Lazy(false) + SwaggerResourceResolver swaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) { + return new SwaggerResourceResolverPlus(swaggerUiConfigProperties); + } + +} + +/** + * 扩展的 SwaggerResourceResolver 类 + */ +class SwaggerResourceResolverPlus extends SwaggerResourceResolver { + + /** + * 构造方法 + * @param swaggerUiConfigProperties Swagger UI 配置属性 + */ + public SwaggerResourceResolverPlus(SwaggerUiConfigProperties swaggerUiConfigProperties) { + super(swaggerUiConfigProperties); + } + + /** + * 解析资源 + * @param exchange ServerWebExchange 对象 + * @param requestPath 请求路径 + * @param locations 资源位置列表 + * @param chain ResourceResolverChain 对象 + * @return 解析后的 Mono 对象 + */ + @Override + public Mono resolveResource(ServerWebExchange exchange, String requestPath, + List locations, ResourceResolverChain chain) { + Mono resolved = chain.resolveResource(exchange, requestPath, locations); + if (!Mono.empty().equals(resolved) && requestPath.startsWith("swagger-ui")) { + String webJarResourcePath = findWebJarResourcePath(requestPath); + if (webJarResourcePath != null) + return chain.resolveResource(exchange, webJarResourcePath, locations); + } + return resolved; } }