• 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
  • 相关阅读:
    [GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
    JupyterLab使用指南(二):JupyterLab基础
    Markdown编辑器语法
    HTML网页大作业代码【免费代码已上传】
    Java并发常见面试题
    C# Winform代码
    Flask入门学习教程
    VScode 入门TypeScript 常见错误
    软件测试/测试开发丨PyCharm安装指南与技巧分享
    如何在 JavaScript 中使用三元运算符
  • 原文地址:https://blog.csdn.net/qq_41829337/article/details/126215072