• Spring Boot项目整合swagger进行接口测试


    swagger的作用
    1. 生成在线接口文档
    2. 方便接口测试
    创建公共模块,整合swagger,使所有模块都能使用
    1. 在guli-parent下创建common模块(maven工程)

    2. 在common中引入相关依赖

          <!--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>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    3. 类型改为pom,因为它下面还有子模块

          <artifactId>common</artifactId>
          <packaging>pom</packaging>
      
      • 1
      • 2
    4. 在common下面创建子模块service-base

      在模块service-base中,创建包com.atguigu.servicebase.config,创建配置类SwaggerConfig

          @Configuration // 配置类
          @EnableSwagger2 // swagger注解,表示做的是swagger整合
          public class SwaggerConfig {
              @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("网站-课程中心API文档")
                          .description("本文档描述了课程中心微服务接口定义")
                          .version("1.0")
                          .contact(new Contact("java", "http://atguigu.com", "1123@qq.com"))
                          .build();
              }
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    5. 在service-edu中引入service-base

      现在的swagger是在common里面,并不是在service-edu中,service-edu中想要用common里的东西,需要在service-edu中引入service-base,为了方便,统一放到service模块中,在pom里引入依赖

          <dependency>
              <groupId>com.atguigu</groupId>
              <artifactId>service-base</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    6. 在启动类里添加注解@ComponentScan

      spring boot默认从外往里扫,因此无法扫描到配置类SwaggerConfig,需要自己设置包扫描规则

      	@ComponentScan(basePackages = {"com.atguigu"}) // 设置包扫描规则,到包com.atguigu里进行扫描
      
      • 1
    定义接口说明和参数说明
    1. 定义在类上

      	@Api(description="讲师管理")
      
      • 1
    2. 定义在方法上

      	@ApiOperation(value = "根据ID删除讲师")
      
      • 1
    3. 定义在参数上

      	@ApiParam(name = "id", value = "讲师ID", required = true)
      
      • 1
    访问swagger
    • swagger访问地址:localhost:8001/swagger-ui.html
  • 相关阅读:
    如何合并为pdf文件?合并为pdf文件的方法
    上班族想轻松快速拿本科证,建议选小自考!
    关于后台权限模块的逻辑
    vue-devtools安装后显示vue.js not detected
    git命令
    xinput1_3.dll丢失怎么修复win10_有什么好的修复方法推荐?
    asp.net 应用程序中同步方法调用异步方法无响应解决方法
    奥迪AUDI EDI 项目中供应商需要了解哪些信息?
    【混合编程】Matlab和C++混编
    优化Scrum敏捷需求管理流程,敏捷需求如何管理。
  • 原文地址:https://blog.csdn.net/qq_41829337/article/details/126215072