【SpringBoot | Swagger】SpringBoot整合Swagger

SpringBoot整合Swagger

1. 什么是Swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。简单说就是项目跑起来了,你的接口可视化展现出来。

2. 怎么在SpringBoot中使用Swagger

1. 配置pom.xml

这里我们导入swagger的相关内容,并额外导入了一个开源的显示界面包,左右布局符合国人界面使用习惯

        <!-- 自动生成API文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- Swagger UI插件 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- 开源的api接口显示界面,左右布局 -->
        <dependency>
            <groupId>com.github.caspar-chen</groupId>
            <artifactId>swagger-ui-layer</artifactId>
            <version>1.1.3</version>
        </dependency>

2. 编写Swagger配置类

package com.xxx.xxx.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 配置Swagger2
 *
 * @author axiang 
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 关闭 swagger,从配置文件中读取内容,这里这么做是将生产环境和开发环境的配置区分开来,毕竟你不会想在实际生产环境中开放接口文档给其他人的
     * @Value 注解的作用可查看百度,这里不多做解释
     */
    @Value("${xxx.swagger.enable}")
    private boolean enableSwagger;
    
    
    private String title = "xxx项目接口文档";
    private String description = "xxx项目接口文档,Spring boot版";
    private String version = "1.0.0";
    private String termsOfServiceUrl = "";
    private String license = "";
    private String licenseUrl = "";

    @Bean
    public Docket api() {
        // 允许开启swagger
        if (enableSwagger) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    // 扫描Controller所在的包
                    .apis(RequestHandlerSelectors.basePackage("xxx.xxx.controller"))
                    .paths(PathSelectors.any())
                    .build();
        } else {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .paths(PathSelectors.none())
                    .build();
        }


    }

    private ApiInfo apiInfo() {
        // 具体内容是什么功能请百度相关文档
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .termsOfServiceUrl(termsOfServiceUrl)
                .license(license)
                .licenseUrl(licenseUrl)
                .build();
    }

}

3. 使用

由于我们采用了第三方界面包,所以可以通过两个路径访问
请注意,第三方界面包依然调用的swagger的接口,所以关闭swagger后第三方界面包不能正常使用哦

# 原装正品请访问
http://[IP地址]:[端口号]/[项目路径]/swagger-ui.html
# 左右布局请访问
http://[IP地址]:[端口号]/[项目路径]/docs.html

猜你喜欢

转载自www.cnblogs.com/axiangcoding/p/11913362.html