From 6c2784a57f2d47e584bda704422211e3476de1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Tue, 26 Jul 2022 23:11:04 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=8E=A5=E5=8F=A3=E8=B7=AF=E5=BE=84=E4=B8=8E=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E8=B7=AF=E5=BE=84=E5=8C=B9=E9=85=8D=20=E5=B9=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9C=8D=E5=8A=A1=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=20contextPath=20=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../doc/config/SwaggerAutoConfiguration.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/ruoyi-common-doc/src/main/java/com/ruoyi/common/doc/config/SwaggerAutoConfiguration.java b/ruoyi-common/ruoyi-common-doc/src/main/java/com/ruoyi/common/doc/config/SwaggerAutoConfiguration.java index 14aa3564..ce14d37c 100644 --- a/ruoyi-common/ruoyi-common-doc/src/main/java/com/ruoyi/common/doc/config/SwaggerAutoConfiguration.java +++ b/ruoyi-common/ruoyi-common-doc/src/main/java/com/ruoyi/common/doc/config/SwaggerAutoConfiguration.java @@ -1,18 +1,23 @@ package com.ruoyi.common.doc.config; -import com.ruoyi.common.doc.handler.OpenApiHandler; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.doc.config.properties.SwaggerProperties; +import com.ruoyi.common.doc.handler.OpenApiHandler; import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Paths; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.security.SecurityRequirement; import lombok.RequiredArgsConstructor; import org.springdoc.core.*; import org.springdoc.core.customizers.OpenApiBuilderCustomizer; +import org.springdoc.core.customizers.OpenApiCustomiser; import org.springdoc.core.customizers.ServerBaseUrlCustomizer; import org.springdoc.core.providers.JavadocProvider; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -33,6 +38,11 @@ public class SwaggerAutoConfiguration { private final SwaggerProperties swaggerProperties; + private final ServerProperties serverProperties; + + @Value("${spring.application.name}") + private String appName; + @Bean @ConditionalOnMissingBean(OpenAPI.class) public OpenAPI openApi() { @@ -75,4 +85,21 @@ public class SwaggerAutoConfiguration { return new OpenApiHandler(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider); } + /** + * 对已经生成好的 OpenApi 进行自定义操作 + */ + @Bean + public OpenApiCustomiser openApiCustomiser() { + String contextPath = serverProperties.getServlet().getContextPath(); + String appPath = "/" + StringUtils.substring(appName, appName.indexOf("-") + 1); + String finalContextPath = StringUtils.isBlank(contextPath) || "/".equals(contextPath) ? appPath : appPath + contextPath; + // 对所有路径增加前置上下文路径 + return openApi -> { + Paths oldPaths = openApi.getPaths(); + Paths newPaths = new Paths(); + oldPaths.forEach((k,v) -> newPaths.addPathItem(finalContextPath + k, v)); + openApi.setPaths(newPaths); + }; + } + }