一、是什么
当下很多公司都采取前后端分离的开发模式,前端和后端的工作由不同的工程师完成。在这种开发模式下,维持一份及时更新且完整的 Rest API 文档将会极大的提高我们的工作效率。传统意义上的文档都是后端开发人员手动编写的,相信大家也都知道这种方式很难保证文档的及时性,这种文档久而久之也就会失去其参考意义,反而还会加大我们的沟通成本。而 Swagger 给我们提供了一个全新的维护 API 文档的方式。
二、为什么要使用它
1、代码变更,文档跟着代码变、只需要少量的注解Swagger就可以根据代码自动的生成API文档,很好的保证了文档的实时性。
2、跨语言,Swagger支持40多种语言。
3、Swagger UI 呈现出来的是一份可以交互的API文档,我们可以直接在文档页面尝试API的调用,省去了准备复杂的调用参数的过程。
4、还可以将文档规范导入相关的工具里面(例如:Postman、SoapUI)、这些工具将会为我们自动地创建自动化测试。
三、怎么用
1、在项目pom.xml里面加入Swagger2相关的依赖
<!--swagger2配置-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>swagger-bootstrap-ui</artifactId>
      <version>1.6</version>
    </dependency>
2、新建Swagger2的配置类
package com.zhouhong.config;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * @ClassName: Swagger2
 * @Description:
 * @Author: 周红
 * @NickName: Tom-shuhu
 * @Date: Created in 2020/12/15
 **/
@Configuration
@EnableSwagger2
public class Swagger2 {
  //  http://localhost:8088/swagger-ui.html 原路径
  //  http://localhost:8088/doc.html 原路径
  //配置swagger2核心配置
  @Bean
  public Docket createRestApi(){
    return new Docket(DocumentationType.SWAGGER_2) //指定api类型位swagger2
      .apiInfo(apiInfo())            //用于定义api文档汇总信息
        .select().apis(RequestHandlerSelectors
            .basePackage("com.zhouhong.controller")) //指定生成文档的controller
        .paths(PathSelectors.any())      
        .build();
  }
  private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
        .title("Tom-shushu 的项目接口api") //文档标题
        .contact(new Contact("周红", //作者
            "www.zhouhong.icu",  
            "15249239025@163.com")) //联系人
        .description("Tom-shushu 的项目api接口")//详细信息
        .version("1.0.0")//文档版本号
        .termsOfServiceUrl("www.zhouhong.icu")//网站地址
        .build();
  }
}
文档配置说明:
a.为任何接口生成API文档,这种方式不必在接口方法上加任何注解,方便的同时也会因为没有添加任何注解所以生成的API文档也没有注释,可读性不高。
 @Bean
  public Docket createRestApi(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        //为任何接口生成API文档
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build();
  }
b.为当前配置的包下controller生成API文档
.apis(RequestHandlerSelectors.basePackage("com.troila"))
c.为有@Api注解的Controller生成API文档
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
d.为有@ApiOperation注解的方法生成API文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
三、常见注解简介
@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个参数描述 @ApiModel:用对象实体来作为入参 @ApiProperty:用对象接实体收参数时,描述对象的一个字段 @ApiResponse:HTTP响应其中1个描述 @ApiResponses:HTTP响应整体描述 @ApiIgnore:使用该注解忽略这个API @ApiError :发生错误返回的信息 @ApiImplicitParam:一个请求参数 @ApiImplicitParams: 多个请求参数
四、演示(为方便我使用了上面第一种配置)
1、使用原路径访问

2、原路径调试

3、doc模式访问

4、doc模式调试

到此这篇关于SpringBoot集成Swagger2的方法的文章就介绍到这了,更多相关SpringBoot集成Swagger2内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
