1、先上一个目录结构 描述一下 项目目录设计

SpringBoot 中使用Swagger2 进行接口调试_swagger

2、SwaggerConfig 类下的内容

package com.school.service_base;

import com.google.common.base.Predicates;
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.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;

/**
 * @User: Json
 * @Date: 2021/11/7
 **/
@Configuration//配置类
@EnableSwagger2 //swagger注解
public class SwaggerConfig {

    //    //整合Swagger2
    //    //创建公共模块 整合 Swagger 全局使用
    // 1. 创建 common 公共模块 并配置使用的依赖
    // 2. 再公共模块中创建 service子模块的公共配置serviceConfig 配置 Swagger2

    //在这里配置后  service模块中怎么使用?
    //1. 在你需要使用的模块中 加入配置依赖
    // 比如你需要在 service 模块中使用 就在 service虚啊的 pom.xml 加以下依赖

    //<!--        引入公共模块创建的 service_base 的配置文件
    //             artifactId 在这个标签中写 会有提示-->
    //        <dependency>
    //            <groupId>com.schoolWeb</groupId>
    //            <artifactId>service_base</artifactId>
    //            <version>0.0.1-SNAPSHOT</version>
    //        </dependency>

    //2. 需要在启动类中加上 包的扫描规则
    // 如果不加的话 他只能扫描当前包下的类 别的模块下的类扫描不到
    // @ComponentScan(basePackages = {"com.school"})
    // 这样就可以扫到 项目中 所有 com.school的包


    // 3. 启动 测试 访问  Swagger
    // 固定地址: http://localhost:8001/swagger-ui.html

    //4. 加注解 让 api 可读性更高
    //类上加 @Api(description = "讲师管理")
    //方法名中加     @ApiOperation("查询所有讲师")
    //参数中加 @ApiParam(name = "id", value = "讲师ID", required = true)

    //以下配置Swagger插件 一般不需要修改 常用配置 复制过去 即可
    // groupName //分组
    // webApiInfo 描述
    //   .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
    //    .paths(Predicates.not(PathSelectors.regex("/error.*")))
    // 包含这些的 不显示 admin error
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("java api学习")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "https://jsonll.blog.csdn.net/", "870422471@qq.com"))
                .build();
    }
}
  1. 对上面的备注描述 一 一 解释
3.1

//    //整合Swagger2  对着上面目录结构进行创建
//    //创建公共模块 整合 Swagger 全局使用
// 1. 创建 common 公共模块 并配置使用的依赖
// 2. 再公共模块中创建 service子模块的公共配置serviceConfig 配置 Swagger2  
3.2

 在 common模块下 的 pom.xml 引入 常用 依赖

<packaging>pom</packaging>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--lombok用来简化实体类:需要安装lombok插件-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <scope>provided </scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <scope>provided </scope>
    </dependency>

    <!-- redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- spring2.X集成redis所需common-pool2
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.0</version>
    </dependency>-->
</dependencies>
3.3

//在这里配置后  service模块中怎么使用?
//1. 在你需要使用的模块中 加入配置依赖
// 比如你需要在 service 模块中使用 就在 service虚啊的 pom.xml 加以下依赖

SpringBoot 中使用Swagger2 进行接口调试_swagger_02

//<!--        引入公共模块创建的 service_base 的配置文件
//             artifactId 在这个标签中写 会有提示-->
//        <dependency>
//            <groupId>com.schoolWeb</groupId>
//            <artifactId>service_base</artifactId>
//            <version>0.0.1-SNAPSHOT</version>
//        </dependency>
//2. 需要在启动类中加上 包的扫描规则
// 如果不加的话 他只能扫描当前包下的类 别的模块下的类扫描不到
// @ComponentScan(basePackages = {"com.school"})
// 这样就可以扫到 项目中 所有 com.school的包

SpringBoot 中使用Swagger2 进行接口调试_spring_03

// 3. 启动 测试 访问  Swagger
// 固定地址: http://localhost:8001/swagger-ui.html
//4. 加注解 让 api 可读性更高
//类上加 @Api(description = "讲师管理")
//方法名中加     @ApiOperation("查询所有讲师")
//参数中加 @ApiParam(name = "id", value = "讲师ID", required = true)