001swagger2--接口文档生成利器

发布时间 2023-11-22 22:33:50作者: Allen_Hao

1. 配置

在springboot整合swagger2

1.1 引入jar包

        <dependency>
<groupId>com.allen.pan</groupId>
<artifactId>allen-pan-core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
<!-- org.springframework.boot:spring-boot-configuration-processor 是 Spring Boot 中的一个模块,
用于处理配置类(Configuration Classes)以及生成与之相关的元数据。
配置类是使用 @Configuration 注解的类,通常用于定义配置信息、Bean 的创建以及其他与应用程序配置相关的内容。

作用:
配置类处理: 该模块用于处理带有 @Configuration 注解的配置类,提取配置信息并生成相应的元数据,以便其他组件能够更容易地理解和利用这些配置。
元数据生成: 生成的元数据可以用于 IDE 的自动补全、静态分析工具的支持等。
Annotation Processor:
spring-boot-configuration-processor 是一个注解处理器(annotation processor)。
在编译时,它会扫描项目中的配置类,分析注解并生成元数据文件,这些文件通常包括 META-INF/spring-configuration-metadata.json。
元数据文件 (spring-configuration-metadata.json):
生成的元数据文件包含了配置类中所有 @ConfigurationProperties 注解的属性信息,包括属性的名称、类型、描述等。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

1.2 配置

1.2.1 新建META-INF/additional-spring-configuration-metadata.json文件

 其内容:

{
"properties": [
{
"name": "swagger2.show",
"type": "java.lang.Boolean",
"description": "是否展示接口文档",
"defaultValue": true
},
{
"name": "swagger2.group-name",
"type": "java.lang.String",
"description": "组名称",
"defaultValue": "r-pan"
},
{
"name": "swagger2.title",
"type": "java.lang.String",
"description": "接口文档标题",
"defaultValue": "r-pan-server"
},
{
"name": "swagger2.description",
"type": "java.lang.String",
"description": "接口文档描述",
"defaultValue": "r-pan-server"
},
{
"name": "swagger2.terms-of-service-url",
"type": "java.lang.String",
"description": "接口文档基础请求路径",
"defaultValue": "http://127.0.0.1:${server.port}"
},
{
"name": "swagger2.base-package",
"type": "java.lang.String",
"description": "接口文档基础接口扫描路径",
"defaultValue": "com.imooc.pan"
},
{
"name": "swagger2.contact-name",
"type": "java.lang.String",
"description": "联系人名称",
"defaultValue": "rubin"
},
{
"name": "swagger2.contact-url",
"type": "java.lang.String",
"description": "联系人地址",
"defaultValue": "https://blog.rubinchu.com"
},
{
"name": "swagger2.contact-email",
"type": "java.lang.String",
"description": "联系人邮箱",
"defaultValue": "rubinchu@126.com"
},
{
"name": "swagger2.version",
"type": "java.lang.String",
"description": "项目版本",
"defaultValue": "1.0"
}
]
}

1.2.2 Properties类

import com.allen.pan.constant.AllenPanConstants;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * swagger2配置属性实体
 * 文件: additional-spring-configuration-metadata.json
 * @see  @see 后面可以跟随类名、方法名或者链接,用于指向其他相关的文档或资源
 */
@Data
@Component
@ConfigurationProperties(prefix = "swagger2")
public class Swagger2ConfigProperties {

    private boolean show = true;

    private String groupName = "allen-pan";

    private String basePackage = AllenPanConstants.BASE_COMPONENT_SCAN_PATH;

    private String title = "allen-pan-server";

    private String description = "allen-pan-server";

    private String termsOfServiceUrl = "http://127.0.0.1:${server.port}";

    private String contactName = "allen";

    private String contactUrl = "https://blog.rubinchu.com";

    private String contactEmail = "allen@126.com";

    private String version = "1.0";

}

 

1.2.3 配置类

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 接口文档配置类
 */
@SpringBootConfiguration
@EnableSwagger2    // 开关
@EnableSwaggerBootstrapUI  // 开关
@Slf4j
public class Swagger2Config {

    @Autowired
    private Swagger2ConfigProperties properties;

    @Bean
    public Docket panServerApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(properties.isShow())
                .groupName(properties.getGroupName())
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
        log.info("The swagger2 have been loaded successfully!");
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .termsOfServiceUrl(properties.getTermsOfServiceUrl())
                .contact(new Contact(properties.getContactName(), properties.getContactUrl(), properties.getContactName()))
                .version(properties.getVersion())
                .build();
    }

}

 

1.3 使用

在其他使用此模块的地方测试