mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-22 20:54:25 +08:00
♻️ Refactoring code. 重构 knife4j 服务发现机制
This commit is contained in:
parent
63b8126a20
commit
9e881598f5
@ -23,6 +23,7 @@
|
|||||||
<git.commit.plugin>4.9.9</git.commit.plugin>
|
<git.commit.plugin>4.9.9</git.commit.plugin>
|
||||||
<spring.checkstyle.plugin>0.0.39</spring.checkstyle.plugin>
|
<spring.checkstyle.plugin>0.0.39</spring.checkstyle.plugin>
|
||||||
<fastjson.version>1.2.83</fastjson.version>
|
<fastjson.version>1.2.83</fastjson.version>
|
||||||
|
<knife4j.version>3.0.3</knife4j.version>
|
||||||
<springdoc.version>2.1.0</springdoc.version>
|
<springdoc.version>2.1.0</springdoc.version>
|
||||||
<swagger.core.version>2.2.14</swagger.core.version>
|
<swagger.core.version>2.2.14</swagger.core.version>
|
||||||
<mybatis-plus.version>3.5.3.2</mybatis-plus.version>
|
<mybatis-plus.version>3.5.3.2</mybatis-plus.version>
|
||||||
@ -103,6 +104,11 @@
|
|||||||
<version>${mysql.version}</version>
|
<version>${mysql.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--springdoc -->
|
<!--springdoc -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.springboot</groupId>
|
||||||
|
<artifactId>knife4j-openapi3-ui</artifactId>
|
||||||
|
<version>${knife4j.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springdoc</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
|
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
|
||||||
|
@ -83,6 +83,11 @@
|
|||||||
<groupId>org.springdoc</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
|
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--引入Knife4j的官方ui包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.springboot</groupId>
|
||||||
|
<artifactId>knife4j-openapi3-ui</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -8,17 +8,27 @@ import com.alibaba.nacos.common.utils.StringUtils;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springdoc.core.properties.AbstractSwaggerUiConfigProperties;
|
import org.springdoc.core.properties.AbstractSwaggerUiConfigProperties;
|
||||||
import org.springdoc.core.properties.SwaggerUiConfigProperties;
|
import org.springdoc.core.properties.SwaggerUiConfigProperties;
|
||||||
|
import org.springdoc.webflux.ui.SwaggerResourceResolver;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SpringDoc配置类,实现InitializingBean接口 swagger 3.0 展示
|
* @author lengleng
|
||||||
*
|
* @date 2022/3/26
|
||||||
|
* <p>
|
||||||
|
* swagger 3.0 展示
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ -34,7 +44,56 @@ public class SpringDocConfiguration implements InitializingBean {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
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<Resource> 对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Mono<Resource> resolveResource(ServerWebExchange exchange, String requestPath,
|
||||||
|
List<? extends Resource> locations, ResourceResolverChain chain) {
|
||||||
|
Mono<Resource> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user