update 优化 生成接口路径与服务路径匹配 并支持服务自定义 contextPath 路径

2.X
疯狂的狮子Li 3 years ago
parent 2176e88712
commit 6c2784a57f

@ -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);
};
}
}

Loading…
Cancel
Save