Swagger集成

Swagger 是一款用于构建、描述、调用和可视化 RESTful API 的开源工具。它可以根据代码自动生成 API 文档,方便开发人员进行接口文档的编写和测试。本文将从多个方面对 Swagger 集成进行详细的阐述。

一、Swagger 简介

Swagger 是由 Tony Tam 在 2011 年创建的一个开源项目。它旨在简化 RESTful API 的开发和文档生成,并且可以通过自动生成的 API 文档来提高沟通效率。Swagger 可以支持多种编程语言和框架。

通常情况下,RESTful API 的开发涉及到以下方面:

  • 定义资源
  • 定义 HTTP 方法
  • 定义认证
  • 定义错误响应
  • 编写业务逻辑代码
  • 编写测试代码
  • 编写文档

这些工作很繁琐,而 Swagger 可以根据代码自动生成 API 文档,从而省去了很多手动编写文档的时间和精力。

二、Swagger 集成

Swagger 的集成非常简单,只需要在项目中加入 Swagger 的相关依赖即可。

1、Maven 集成

使用 Maven 构建项目时,在 pom.xml 文件的依赖中加入以下内容即可:

  <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
  </dependency>

  <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
  </dependency>

同时,在启动类中加入注解 @EnableSwagger2 即可启用 Swagger。

  package com.example.demo;

  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import springfox.documentation.swagger2.annotations.EnableSwagger2;

  @SpringBootApplication
  @EnableSwagger2
  public class DemoApplication {

      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }

  }

启动应用,访问 http://localhost:8080/swagger-ui.html 即可看到 Swagger 自动化生成的 API 文档。

2、Spring Boot 集成

在 Spring Boot 中集成 Swagger 更加方便,只需要加入以下 Maven 依赖即可:

  <dependency>
      <groupId>springfox.documentation</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
  </dependency>

  <dependency>
      <groupId>springfox.documentation</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
  </dependency>

然后在 Application 启动类中加入注解 @EnableSwagger2 即可启用 Swagger。

  package com.example.demo;

  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import springfox.documentation.swagger2.annotations.EnableSwagger2;

  @SpringBootApplication
  @EnableSwagger2
  public class DemoApplication {

      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }

  }

启动应用后,访问 http://localhost:8080/swagger-ui.html 即可查看生成的 API 文档。

三、配置与使用

Swagger 集成之后,我们需要对其进行配置和使用。

1、配置

Swagger 配置很灵活,可以进行各种自定义设置。

我们可以通过在配置类中加入 Bean 对象来配置一些 Swagger 的默认信息。

  package com.example.demo.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;

  @Configuration
  @EnableSwagger2
  public class SwaggerConfig {

      @Bean
      public Docket api() {
          return new Docket(DocumentationType.SWAGGER_2)
                  .select()
                  .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                  .paths(PathSelectors.any())
                  .build()
                  .apiInfo(apiInfo());
      }

      private ApiInfo apiInfo() {
          return new ApiInfoBuilder()
                  .title("Demo API")
                  .description("Demo API Doc")
                  .version("1.0")
                  .contact(new Contact("Swagger", "https://swagger.io", ""))
                  .build();
      }

  }

在这个配置文件中,我们通过 Docket 类进行了各种设置,包括扫描控制器包、显示文档的路径、设置 API 相关信息等。可以看到其中设置了 API 的标题、描述、版本、联系信息等。通过 Bean 注解将这个 Docket 对象放入 Spring 容器中,就能够自动生效。

2、使用

Swagger 使用非常简单,只需要在控制器上加入一些注解即可。

首先,我们需要在控制器类上加入 @Api 注解,标识这是一个 API 控制器,同时可以设置一些额外信息。

  package com.example.demo.controller;

  import io.swagger.annotations.Api;
  import io.swagger.annotations.ApiOperation;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;

  @RestController
  @Api(value = "DemoController", tags = {"Demo"})
  @RequestMapping("/demo")
  public class DemoController {

      @ApiOperation(value = "Hello World API", notes = "返回 Hello World")
      @GetMapping("/hello")
      public String hello() {
          return "Hello World!";
      }

  }

在 hello 方法上加入 @ApiOperation 注解,即可设置其请求方法、接口描述、接口参数、返回值等信息。其中 value 表示接口的名称,notes 表示接口的描述。

这样,Swagger 就会自动通过扫描控制器类和注解信息来绘制出可视化的 API 文档。

四、结语

本文对 Swagger 集成进行了详细的阐述,包括 Swagger 的简介、集成、配置和使用。Swagger 作为一款非常好用的工具,在 RESTful API 的开发和文档编写时有着很大的作用和价值。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/186359.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-11-27 05:46
下一篇 2024-11-27 05:46

相关推荐

  • Swagger常用注解详解

    Swagger是一个广泛使用的API文档工具,它可以根据代码自动生成API文档,并提供交互式的API测试界面。在Swagger中,注解不仅仅只是用来生成文档,还能够控制API的行为…

    编程 2025-04-23
  • Swagger的使用方法详解

    一、Swagger简介 Swagger是一种流行的API开发工具,它可以用来生成和管理RESTful服务的API文档,并允许用户通过UI界面来互动性地测试API请求和响应。Swag…

    编程 2025-02-25
  • 全面了解Swagger-Resources

    Swagger-Resources是一个用于聚合Swagger API资源的库,它提供了一个RESTful API和一组Java接口,可以用于管理和获取API的元数据。下面将从多个…

    编程 2025-02-05
  • 深入理解Swagger依赖

    Swagger是一个开源项目,用于描述Restful API的工具。Swagger的依赖可以方便地使用它的多种功能,包括API描述、API测试和API文档生成。本文将从不同的角度为…

    编程 2025-02-01
  • 深入了解Flask Swagger

    一、什么是Flask Swagger? Flask Swagger是Flask框架中的一个扩展,它提供了一种简单的方法来构建和维护RESTful API文档。它基于现有的swagg…

    编程 2025-02-01
  • 详解Swagger注释

    一、Swagger注释是什么 Swagger注释是一种文本注释格式,它用来描述API的各种信息,如API的请求参数、响应结果、错误信息等。有了Swagger注释,我们可以用各种工具…

    编程 2025-01-16
  • Swagger教程详解

    一、Swagger是什么? Swagger是一个面向RESTful API的开源软件框架。它提供了一组工具,可以帮助您描述、生成、消费API。通过使用Swagger,您可以更好地了…

    编程 2025-01-04
  • 了解Swagger默认地址

    Swagger 是一种非常流行的API文档工具,提供了许多有用的功能,其中包括自动生成API文档、API测试、在线接口调试等。通过使用Swagger,开发人员可以更加方便地了解和使…

    编程 2024-12-30
  • Swagger Editor:完美的API文档化利器

    Swagger Editor是一款产生在完善RESTful API的时代下,由swagger.io发起的开源项目。它允许开发者使用简洁的语法来编写开放API的文档,帮助你更快地将A…

    编程 2024-12-27
  • Swagger-resources404:如何解决Swagger页面404错误

    一、什么是swagger-resources404错误 在使用Swagger时,有时候会遇到swagger-resources404错误。这个错误的出现通常是因为Swagger的资…

    编程 2024-12-23

发表回复

登录后才能评论