Swagger 简介

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。

简单的来讲,Swagger 就是一个可以自动生成 接口文档** **的工具。

快速开始

1. 新建 SpringBoot 项目并创建对应的 Controller 层

image.png

2. 在 Maven 中加入对 Swagger 的依赖

  1. <!-- Swagger Api文档生成框架 -->
  2. <!-- Swagger2 -->
  3. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  4. <dependency>
  5. <groupId>io.springfox</groupId>
  6. <artifactId>springfox-swagger2</artifactId>
  7. <version>2.9.2</version>
  8. </dependency>
  9. <!-- Swagger 生成的接口文档 -->
  10. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  11. <dependency>
  12. <groupId>io.springfox</groupId>
  13. <artifactId>springfox-swagger-ui</artifactId>
  14. <version>2.9.2</version>
  15. </dependency>

3. 创建 Swagger 的配置类

image.png
注:

  1. @EnableSwagger2:开启 Swagger,使用该注释时应附带注解 @Configuration

    4. 打开 Swagger 生成的接口文档


    地址:项目地址后 + swagger-ui.html
    例:http://localhost:8080/swagger-ui.html

    显示
    image.png

配置 Swagger 接口信息

1. 代码

  1. @Configuration
  2. /** 开启 Swagger */
  3. @EnableSwagger2
  4. public class SwaggerConfig {
  5. /**
  6. * 将 Swagger 的主要接口 docket 注入 bean
  7. */
  8. @Bean
  9. public Docket docket() {
  10. return new Docket(DocumentationType.SWAGGER_2)
  11. .apiInfo(apiInfo());
  12. }
  13. /**
  14. * 配置 Swagger 信息
  15. */
  16. private ApiInfo apiInfo() {
  17. // 作者信息
  18. Contact contact = new Contact(
  19. "yafnds",
  20. "https://github.com/y19991",
  21. "yafnds@163.com"
  22. );
  23. return new ApiInfo(
  24. "Swagger 接口文档",
  25. "接口文档",
  26. "v1.0",
  27. "https://github.com/y19991",
  28. contact,
  29. "Apache 2.0",
  30. "http://www.apache.org/licenses/LICENSE-2.0",
  31. new ArrayList<VendorExtension>()
  32. );
  33. }
  34. }

2. 构造器参数

  1. 作者信息

    1. name:作者名称
    2. url:作者主页
    3. email:作者邮箱
      1. new Contact( String name, String url, String email )
  2. Swagger 信息

    1. title:Api 标题
    2. description:Api 概述
    3. version:Api 版本号
    4. termsOfServiceUrl:创建该 Api 的团队主页
    5. contact:作者信息
    6. license:开源许可证(一般固定填 Apache 2.0
    7. licenseUrl:许可证地址(一般固定填 http://www.apache.org/licenses/LICENSE-2.0
      1. public ApiInfo(
      2. String title,
      3. String description,
      4. String version,
      5. String termsOfServiceUrl,
      6. Contact contact,
      7. String license,
      8. String licenseUrl,
      9. Collection<VendorExtension> vendorExtensions) {
      10. this.title = title;
      11. this.description = description;
      12. this.version = version;
      13. this.termsOfServiceUrl = termsOfServiceUrl;
      14. this.contact = contact;
      15. this.license = license;
      16. this.licenseUrl = licenseUrl;
      17. this.vendorExtensions = newArrayList(vendorExtensions);
      18. }

      3. 效果

      image.png

过滤扫描的接口

1. 代码

  1. @Bean
  2. public Docket docket() {
  3. return new Docket(DocumentationType.SWAGGER_2)
  4. .apiInfo(apiInfo())
  5. .select()
  6. /*
  7. apis: 定义要包含的类
  8. RequestHandlerSelectors 配置要扫描接口的方式
  9. basePackage: 指定要扫描的包
  10. any():扫描全部
  11. none():都不扫描
  12. withClassAnnotation(Controller.class):扫描类的注解,参数是一个注解的反射对象
  13. withMethodAnnotation:扫描方法上的注解
  14. */
  15. //.apis(RequestHandlerSelectors.basePackage("com.yafnds.springbootwork02swagger.controller"))
  16. /*
  17. paths: 根据路径映射定义应包含哪个控制器的方法
  18. PathSelectors 路径选择器
  19. ant() 匹配所有
  20. none() 全不匹配
  21. regex(String pathRegex) 根据正则匹配
  22. ant(String antPattern) ant 风格的匹配模式
  23. */
  24. //.paths(PathSelectors.ant("/hello/**"))
  25. .paths(PathSelectors.any())
  26. .build();
  27. }

2. 方法说明

  1. select( ):初始化 api 选择构建器
  2. build( ):构建器配置完成,开始执行

  3. apis( Predicate<_RequestHandler> _selector ):选择要包含的类

    1. RequestHandlerSelectors:配置要扫描接口的方式
      1. basePackage:指定要扫描的包
      2. any( ):扫描全部
      3. none( ):都不扫描
      4. withClassAnnotation( Class<? extends Annotation_> _annotation ):扫描 类的注解,参数是 目标注解的反射对象
      5. withMethodAnnotation( Class<? extends Annotation_> _annotation ):扫描方法上的注解
  4. paths( Predicate<_String> _selector ):根据路径映射定义应包含哪个控制器的方法

    1. PathSelectors 地址选择器
      1. any( ):全部匹配
      2. none( ):都不匹配
      3. regex( String pathRegex ):根据正则匹配
      4. ant( String antPattern ):ant 风格的匹配模式

其他方法

1. 关闭 Swagger

  1. public Docket docket() {
  2. return new Docket(DocumentationType.SWAGGER_2)
  3. .apiInfo(apiInfo())
  4. .enable(false)
  5. .select()
  6. ...;
  7. }

说明:enable 配置是否启用 Swagger,如果是 false,在浏览器将无法访问
效果:
image.png
**

2. 组名

  1. public Docket docket() {
  2. return new Docket(DocumentationType.SWAGGER_2)
  3. .apiInfo(apiInfo())
  4. .groupName("yafnds")
  5. .select()
  6. ...;
  7. }

说明:如果存在多个Docket实例,则每个实例都必须有由该方法提供的唯一groupName。默认为“default”。

3. 指定启动 Swagger 的环境

:只有在开发环境中,才启动 Swagger
代码

  1. @Bean
  2. public Docket docket(Environment environment) {
  3. // 设置要显示的 Swagger 环境
  4. Profiles profiles = Profiles.of("dev");
  5. // 通过 environment.acceptsProfiles 判断是否处在自己设定的环境当中
  6. boolean flag = environment.acceptsProfiles(profiles);
  7. return new Docket(DocumentationType.SWAGGER_2)
  8. .apiInfo(apiInfo())
  9. .enable(flag)
  10. .select()
  11. ...;
  12. }