升级 Knife4j 4.1.0 版本

pull/33/head
YunaiV 2023-04-14 09:23:03 +08:00
parent ca9656a40f
commit f1330bf4e8
4 changed files with 13 additions and 150 deletions

View File

@ -23,7 +23,7 @@
<servlet.versoin>2.5</servlet.versoin> <servlet.versoin>2.5</servlet.versoin>
<swagger.version>2.2.8</swagger.version> <swagger.version>2.2.8</swagger.version>
<springdoc.version>1.6.14</springdoc.version> <springdoc.version>1.6.14</springdoc.version>
<knife4j.version>4.0.0</knife4j.version> <knife4j.version>4.1.0</knife4j.version>
<!-- DB 相关 --> <!-- DB 相关 -->
<druid.version>1.2.15</druid.version> <druid.version>1.2.15</druid.version>
<mybatis-plus.version>3.5.3.1</mybatis-plus.version> <mybatis-plus.version>3.5.3.1</mybatis-plus.version>

View File

@ -1,105 +0,0 @@
package cn.iocoder.yudao.gateway.swagger;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import java.util.*;
/**
* Swagger {@link HandlerFunction}
*
* @author zxliu
* @since 2022-10-25 11:23
*/
@RequiredArgsConstructor
@Slf4j
public class SwaggerResourceHandlerFunction implements HandlerFunction<ServerResponse> {
private final GatewayProperties gatewayProperties;
@Override
public Mono<ServerResponse> handle(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(getSwaggerResourceList());
}
/**
* SwaggerResource
*
* @return SwaggerResource
*/
public List<Map<String, String>> getSwaggerResourceList() {
// 将 RouteDefinition 转换成 SwaggerResource
List<Map<String, String>> resources = new ArrayList<>();
Set<String> serviceNames = new HashSet<>(); // 已处理的服务名,避免重复
gatewayProperties.getRoutes().forEach(route -> {
// 已存在的服务,直接忽略
String serviceName = route.getUri().getHost();
if (StrUtil.isEmpty(serviceName)) {
return;
}
if (!serviceNames.add(serviceName)) {
return;
}
// 获得 Path PredicateDefinition
String path = getRoutePath(route);
if (path == null) {
return;
}
// 重要:构建最终的 SwaggerResource 对象
resources.add(buildSwaggerResource(serviceName, path));
});
return resources;
}
private Map<String, String> buildSwaggerResource(String name, String location) {
Map<String, String> swaggerResource = new HashMap<>();
swaggerResource.put("name", name);
swaggerResource.put("location", location);
swaggerResource.put("url", location);
swaggerResource.put("swaggerVersion", "3.0.3");
return swaggerResource;
}
/**
* Path
*
*
* predicates:
* - Path=/admin-api/system/**
*
* /admin-api/system/v3/api-docs
*
* @param route
* @return
*/
private String getRoutePath(RouteDefinition route) {
PredicateDefinition pathDefinition = CollUtil.findOne(route.getPredicates(),
predicateDefinition -> "Path".equals(predicateDefinition.getName()));
if (pathDefinition == null) {
log.info("[get][Route({}) 没有 Path 条件,忽略接口文档]", route.getId());
return null;
}
String path = pathDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0");
if (StrUtil.isEmpty(path)) {
log.info("[get][Route({}) Path 的值为空,忽略接口文档]", route.getId());
return null;
}
return path.replace("/**", "/v3/api-docs");
}
}

View File

@ -1,42 +0,0 @@
package cn.iocoder.yudao.gateway.swagger;
import com.github.xiaoymin.knife4j.spring.gateway.configuration.Knife4jGatewayAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
/**
* Swagger
*
* {@link Knife4jGatewayAutoConfiguration} {@link SwaggerResourceHandlerFunction}
* spring.cloud.gateway.routes Swagger
*
* Knif4j URL
*
* @see <a href="https://doc.xiaominfo.com/docs/middleware-sources/spring-cloud-gateway/spring-gateway-introduction">Knife4j + Spring Cloud Gateway </a>
*
* @author
*/
@Configuration
@ConditionalOnProperty(name = "knife4j.gateway.enable", havingValue = "true")
@Slf4j
public class YudaoSwaggerAutoConfiguration {
/**
* Swagger URL
*/
public static final String GATEWAY_SWAGGER_GROUP_URL = "/swagger-resources";
@Bean
public RouterFunction<ServerResponse> swaggerResourceHandlerFunction(GatewayProperties gatewayProperties) {
log.info("[swaggerResourceHandlerFunction][初始化完成]");
SwaggerResourceHandlerFunction handlerFunction = new SwaggerResourceHandlerFunction(gatewayProperties);
return RouterFunctions.route().GET(GATEWAY_SWAGGER_GROUP_URL, handlerFunction).build();
}
}

View File

@ -55,6 +55,16 @@ spring:
prefix-enabled: false # 避免 Swagger 重复带上额外的 /admin-api/system 前缀 prefix-enabled: false # 避免 Swagger 重复带上额外的 /admin-api/system 前缀
knife4j: knife4j:
# 聚合 Swagger 文档 # 聚合 Swagger 文档,参考 https://doc.xiaominfo.com/docs/action/springcloud-gateway 文档
gateway: gateway:
enable: true enabled: true
routes:
- name: system-server
service-name: system-server
url: /admin-api/system/v3/api-docs
- name: infra-server
service-name: infra-server
url: /admin-api/infra/v3/api-docs
- name: bpm-server
service-name: bpm-server
url: /admin-api/bpm/v3/api-docs