mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-22 20:54:25 +08:00
♻️ Refactoring code. 重构 common-swagger 模块
This commit is contained in:
parent
52f0e9ed42
commit
4b03d61d18
@ -16,10 +16,12 @@
|
|||||||
|
|
||||||
package com.pig4cloud.pig.common.swagger.annotation;
|
package com.pig4cloud.pig.common.swagger.annotation;
|
||||||
|
|
||||||
|
import com.pig4cloud.pig.common.core.factory.YamlPropertySourceFactory;
|
||||||
import com.pig4cloud.pig.common.swagger.config.OpenAPIDefinitionImportSelector;
|
import com.pig4cloud.pig.common.swagger.config.OpenAPIDefinitionImportSelector;
|
||||||
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
|
import com.pig4cloud.pig.common.swagger.support.SwaggerProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
@ -35,6 +37,7 @@ import java.lang.annotation.*;
|
|||||||
@Inherited
|
@Inherited
|
||||||
@EnableConfigurationProperties(SwaggerProperties.class)
|
@EnableConfigurationProperties(SwaggerProperties.class)
|
||||||
@Import(OpenAPIDefinitionImportSelector.class)
|
@Import(OpenAPIDefinitionImportSelector.class)
|
||||||
|
@PropertySource(value = "classpath:openapi-config.yaml", factory = YamlPropertySourceFactory.class)
|
||||||
public @interface EnablePigDoc {
|
public @interface EnablePigDoc {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,9 +42,9 @@ import java.util.List;
|
|||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* 禁用方法1:使用注解@Profile({"dev","test"})
|
* 禁用方法1:使用注解@Profile({"dev","test"})
|
||||||
*
|
* <p>
|
||||||
* 表示在开发或测试环境开启,而在生产关闭。(推荐使用) 禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable",
|
* 表示在开发或测试环境开启,而在生产关闭。(推荐使用) 禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable",
|
||||||
*
|
* <p>
|
||||||
* havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启,生产环境不填则默认关闭Swagger.
|
* havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启,生产环境不填则默认关闭Swagger.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
@ -72,7 +72,7 @@ public class OpenAPIDefinition extends OpenAPI implements InitializingBean, Appl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() throws Exception {
|
||||||
SwaggerProperties swaggerProperties = applicationContext.getBean(SwaggerProperties.class);
|
SwaggerProperties swaggerProperties = applicationContext.getBean(SwaggerProperties.class);
|
||||||
this.info(new Info().title(swaggerProperties.getTitle()));
|
this.info(new Info().title(swaggerProperties.getTitle()));
|
||||||
// oauth2.0 password
|
// oauth2.0 password
|
||||||
|
@ -6,7 +6,8 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|||||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* openapi 配置类
|
* openapi 配置类
|
||||||
@ -16,33 +17,31 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
public class OpenAPIDefinitionImportSelector implements ImportBeanDefinitionRegistrar {
|
public class OpenAPIDefinitionImportSelector implements ImportBeanDefinitionRegistrar {
|
||||||
|
|
||||||
/**
|
|
||||||
* 注册Bean定义方法
|
|
||||||
* @param metadata 注解元数据
|
|
||||||
* @param registry Bean定义注册器
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
|
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
|
||||||
Optional.ofNullable(metadata.getAnnotationAttributes(EnablePigDoc.class.getName(), true))
|
|
||||||
.map(attrs -> attrs.get("value"))
|
|
||||||
.ifPresent(value -> {
|
|
||||||
createBeanDefinition(registry, "openAPIMetadataRegister", OpenAPIMetadataRegister.class, value);
|
|
||||||
createBeanDefinition(registry, "openAPIDefinition", OpenAPIDefinition.class, value);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(EnablePigDoc.class.getName(), true);
|
||||||
* 创建Bean定义
|
Object value = annotationAttributes.get("value");
|
||||||
* @param registry Bean定义注册器
|
if (Objects.isNull(value)) {
|
||||||
* @param beanName Bean名称
|
return;
|
||||||
* @param beanClass Bean类
|
}
|
||||||
* @param value Bean属性值
|
|
||||||
*/
|
BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(OpenAPIDefinition.class);
|
||||||
private void createBeanDefinition(BeanDefinitionRegistry registry, String beanName, Class<?> beanClass,
|
definition.addPropertyValue("path", value);
|
||||||
Object value) {
|
definition.setPrimary(true);
|
||||||
BeanDefinitionBuilder beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(beanClass);
|
|
||||||
beanDefinition.addPropertyValue("path", value);
|
registry.registerBeanDefinition("openAPIDefinition", definition.getBeanDefinition());
|
||||||
registry.registerBeanDefinition(beanName, beanDefinition.getBeanDefinition());
|
|
||||||
|
// 如果是微服务架构则,引入了服务发现声明相关的元数据配置
|
||||||
|
Object isMicro = annotationAttributes.getOrDefault("isMicro", true);
|
||||||
|
if (isMicro.equals(false)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BeanDefinitionBuilder openAPIMetadata = BeanDefinitionBuilder
|
||||||
|
.genericBeanDefinition(OpenAPIMetadataConfiguration.class);
|
||||||
|
openAPIMetadata.addPropertyValue("path", value);
|
||||||
|
registry.registerBeanDefinition("openAPIMetadata", openAPIMetadata.getBeanDefinition());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.pig4cloud.pig.common.swagger.config;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lengleng
|
||||||
|
* @date 2023/1/4
|
||||||
|
*/
|
||||||
|
public class OpenAPIMetadataConfiguration implements InitializingBean, ApplicationContextAware {
|
||||||
|
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
|
||||||
|
String[] beanNamesForType = applicationContext.getBeanNamesForType(ServiceInstance.class);
|
||||||
|
|
||||||
|
if (beanNamesForType.length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ServiceInstance serviceInstance = applicationContext.getBean(ServiceInstance.class);
|
||||||
|
serviceInstance.getMetadata().put("spring-doc", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,42 +0,0 @@
|
|||||||
package com.pig4cloud.pig.common.swagger.config;
|
|
||||||
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author lengleng
|
|
||||||
* @date 2023/1/4
|
|
||||||
*/
|
|
||||||
public class OpenAPIMetadataRegister implements InitializingBean, ApplicationContextAware {
|
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
@Setter
|
|
||||||
private String path;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置应用程序上下文
|
|
||||||
* @param applicationContext 应用程序上下文
|
|
||||||
* @throws BeansException 可能抛出的异常
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在属性设置后执行的初始化方法
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
// 获取ServiceInstance实例
|
|
||||||
ServiceInstance serviceInstance = applicationContext.getBean(ServiceInstance.class);
|
|
||||||
// 设置元数据
|
|
||||||
serviceInstance.getMetadata().put("spring-doc", path);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,7 @@
|
|||||||
|
# swagger 配置
|
||||||
|
swagger:
|
||||||
|
enabled: true
|
||||||
|
title: Pig Swagger API
|
||||||
|
gateway: http://${GATEWAY-HOST:pig-gateway}:${GATEWAY-PORT:9999}
|
||||||
|
token-url: ${swagger.gateway}/admin/oauth2/token
|
||||||
|
scope: server
|
Loading…
Reference in New Issue
Block a user