|
| 1 | +package top.cadecode.sra.framework.config.core; |
| 2 | + |
| 3 | +import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; |
| 4 | +import lombok.Data; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.beans.BeansException; |
| 7 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 8 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 9 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 10 | +import org.springframework.context.ApplicationContext; |
| 11 | +import org.springframework.context.ApplicationContextAware; |
| 12 | +import org.springframework.context.ConfigurableApplicationContext; |
| 13 | +import org.springframework.context.annotation.Configuration; |
| 14 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 15 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 16 | +import springfox.documentation.builders.ApiInfoBuilder; |
| 17 | +import springfox.documentation.builders.RequestHandlerSelectors; |
| 18 | +import springfox.documentation.service.ApiInfo; |
| 19 | +import springfox.documentation.service.Contact; |
| 20 | +import springfox.documentation.spi.DocumentationType; |
| 21 | +import springfox.documentation.spring.web.plugins.Docket; |
| 22 | +import springfox.documentation.swagger2.annotations.EnableSwagger2; |
| 23 | + |
| 24 | +import javax.annotation.PostConstruct; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Cade Li |
| 30 | + * @date 2021/8/23 |
| 31 | + * @description Swagger2 统一配置类 |
| 32 | + */ |
| 33 | +@Slf4j |
| 34 | +@Data |
| 35 | +@EnableSwagger2 |
| 36 | +@EnableKnife4j |
| 37 | +@Configuration |
| 38 | +@ConfigurationProperties("sra.swagger") |
| 39 | +@ConditionalOnProperty(name = "sra.config.swagger-on", havingValue = "true") |
| 40 | +public class SwaggerConfig implements WebMvcConfigurer, ApplicationContextAware { |
| 41 | + |
| 42 | + private ConfigurableApplicationContext applicationContext; |
| 43 | + |
| 44 | + private final SRAMainConfig mainConfig; |
| 45 | + |
| 46 | + /** |
| 47 | + * 模块和包名 MAP |
| 48 | + */ |
| 49 | + private Map<String, String> module; |
| 50 | + |
| 51 | + /** |
| 52 | + * 文档标题 |
| 53 | + */ |
| 54 | + private String title; |
| 55 | + |
| 56 | + /** |
| 57 | + * 文档描述 |
| 58 | + */ |
| 59 | + private String description; |
| 60 | + |
| 61 | + /** |
| 62 | + * 联系人 |
| 63 | + */ |
| 64 | + private String name; |
| 65 | + |
| 66 | + /** |
| 67 | + * 项目地址 |
| 68 | + */ |
| 69 | + private String url; |
| 70 | + |
| 71 | + /** |
| 72 | + * 邮箱 |
| 73 | + */ |
| 74 | + private String email; |
| 75 | + |
| 76 | + /** |
| 77 | + * 配置 Swagger Docket |
| 78 | + */ |
| 79 | + @PostConstruct |
| 80 | + public void init() { |
| 81 | + log.info("开始配置 Swagger dockets"); |
| 82 | + if (Objects.isNull(module) || Objects.isNull(applicationContext)) { |
| 83 | + log.info("没有找到 Swagger dockets 相关配置"); |
| 84 | + return; |
| 85 | + } |
| 86 | + // 获取 BeanFactory,动态注册 Docket |
| 87 | + ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); |
| 88 | + module.forEach((group, packageName) -> { |
| 89 | + Docket docket = new Docket(DocumentationType.SWAGGER_2) |
| 90 | + // 设置文档信息 |
| 91 | + .apiInfo(apiInfo()) |
| 92 | + .select() |
| 93 | + // 设置扫描包 |
| 94 | + .apis(RequestHandlerSelectors.basePackage(packageName)) |
| 95 | + .build() |
| 96 | + // 设置分组名称 |
| 97 | + .groupName(group); |
| 98 | + beanFactory.registerSingleton(group + "Docket", docket); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * 配置文档信息 |
| 104 | + */ |
| 105 | + public ApiInfo apiInfo() { |
| 106 | + return new ApiInfoBuilder() |
| 107 | + // 设置标题 |
| 108 | + .title(title) |
| 109 | + // 设置描述 |
| 110 | + .description(description) |
| 111 | + // 设置联系方法 |
| 112 | + .contact(new Contact(name, url, email)) |
| 113 | + // 设置版本号 |
| 114 | + .version(mainConfig.getVersion()) |
| 115 | + .build(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * 添加静态资源,映射 Swagger 网页文件 |
| 120 | + */ |
| 121 | + @Override |
| 122 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| 123 | + registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); |
| 124 | + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 129 | + if (applicationContext instanceof ConfigurableApplicationContext) { |
| 130 | + this.applicationContext = (ConfigurableApplicationContext) applicationContext; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments