-
Notifications
You must be signed in to change notification settings - Fork 961
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactoring code. 重构 common-swagger 模块
- Loading branch information
lbw
committed
Aug 29, 2023
1 parent
cca691b
commit 64903a4
Showing
5 changed files
with
118 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...rc/main/java/com/pig4cloud/pig/common/swagger/config/OpenAPIDefinitionImportSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.pig4cloud.pig.common.swagger.config; | ||
|
||
import com.pig4cloud.pig.common.swagger.annotation.EnablePigDoc; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* openapi 配置类 | ||
* | ||
* @author lengleng | ||
* @date 2023/1/1 | ||
*/ | ||
public class OpenAPIDefinitionImportSelector implements ImportBeanDefinitionRegistrar { | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { | ||
|
||
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(EnablePigDoc.class.getName(), true); | ||
Object value = annotationAttributes.get("value"); | ||
if (Objects.isNull(value)) { | ||
return; | ||
} | ||
|
||
BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(OpenAPIDefinition.class); | ||
definition.addPropertyValue("path", value); | ||
|
||
registry.registerBeanDefinition("openAPIDefinition", definition.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()); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...r/src/main/java/com/pig4cloud/pig/common/swagger/config/OpenAPIMetadataConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 { | ||
ServiceInstance serviceInstance = applicationContext.getBean(ServiceInstance.class); | ||
serviceInstance.getMetadata().put("spring-doc", path); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters