当前位置: 首页 > news >正文

【接口设计】用 Swagger 实现接口文档

用 Swagger 实现接口文档

  • 1.配置 Swagger
    • 1.1 添加 Swagger 依赖
    • 1.2 创建 Swagger 配置类
  • 2.编写接口文档

在项目开发中,一般都是由前后端工程师共同定义接口,编写接口文档,之后大家根据这个接口文档进行开发、维护。为了便于编写和维护稳定,可以使用 Swagger 来编写 API 接口文档,以提升团队的沟通效率。

下面演示如何在 Spring Boot 中继承 Swagger。

1.配置 Swagger

1.1 添加 Swagger 依赖

<!--Swagger依赖-->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency><!--Swagger-UI依赖 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
</dependency>

1.2 创建 Swagger 配置类

创建 Swagger 配置类,完成相关配置项,见以下代码:

package com.example.demo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;/*** Swagger 配置类* 在与 Spring Boot 集成时,放在与 Application.java 同级的目录下*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {/*** 创建 API 应用* 本例采用指定扫描的包路径来定义指定要建立 API 的目录*/@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).paths(PathSelectors.any()).build();}/*** 创建该 API 的基本信息(这些基本信息会展现在文档页面中)* 访问地址:http://项目实际地址/swagger-ui.html*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title(" RESTful APIs").description("RESTful APIs").termsOfServiceUrl("http://localhost:8080/").contact("pipi").version("1.0").build();}
}
  • @Configuration:让 Spring 来加载该类配置。
  • @EnableSwagger2:启用 Swagger2.createRestApi 函数创建 Docket 的 Bean。
  • apiInfo():用来展示该 API 的基本信息。
  • select():返回一个 ApiSelectorBuilder 实例,用来控制哪些接口暴露给 Swagger 来展现。
  • apis(RequestHandlerSelectors.basePackage()):配置包扫描路径。Swagger 会扫描包下所有 Controler 定义的 API,并产生文档内容。如果不想产生 API,则使用注解 @ApiIgnore

2.编写接口文档

在完成上述配置后,即生成了文档,但是这样生成的文档主要针对请求本身,而描述自动根据方法等命名产生,对用户并不友好。所以,通常需要自己增加一些说明以丰富文档内容。可以通过以下注解来增加说明。

  • @Api:描述类/接口的主要用途。
  • @ApiOperation:描述方法用途,给 API 增加说明。
  • @ApiImplicitParam:描述方法的参数,给参数增加说明。
  • @ApiImplicitParams:描述方法的参数(Multi-Params),给参数增加说明。
  • @ApiIgnore:忽略某类/方法/参数的文档。

具体使用方法见以下代码:

package com.example.demo.controller;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;@RestController
public class HelloWorldController {@ApiOperation(value = "hello", notes = "notes")@RequestMapping("/hello")public String hello() throws Exception {return "HelloWorld ,Spring Boot!";}// 使用该注解忽略这个 API@ApiIgnore@RequestMapping(value = "/ignoreApi")public String  ignoreApi() {return "HelloWorld ,Spring Boot!";}
}

完成上述代码后,启动项目,访问 http://localhost:8080/swagger-ui.html 就能看到所展示的 RESTful API 的页面,可以通过单击具体的 API 测试请求,来查看代码中配置的信息,以及参数的描述信息。

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/399603.html

相关文章:

  • TensorFlow系列:第四讲:MobileNetV2实战
  • Redis+Caffeine 实现两级缓存实战
  • SpringBoot:SpringBoot中如何实现对Http接口进行监控
  • STM32-I2C硬件外设
  • 暑假第一次作业
  • 【算法专题】快速排序
  • debian 12 PXE Server 批量部署系统
  • 【Pytorch】RNN for Image Classification
  • 基于Java的飞机大战游戏的设计与实现论文
  • 初识影刀:EXCEL根据部门筛选低值易耗品
  • nginx的四层负载均衡实战
  • 中职网络安全B模块Cenots6.8数据库
  • BGP笔记的基本概要
  • 【Redis】复制(Replica)
  • 封装了一个仿照抖音效果的iOS评论弹窗
  • 【JavaWeb程序设计】Servlet(二)
  • php探针
  • 泰勒级数 (Taylor Series) 动画展示 包括源码
  • 蔚来汽车:拥抱TiDB,实现数据库性能与稳定性的飞跃
  • 【Django+Vue3 线上教育平台项目实战】构建高效线上教育平台之首页模块
  • 对比 UUIDv1 和 UUIDv6
  • 记一次饱经挫折的阿里云ROS部署经历
  • 代码运行故障排除:PyCharm中的问题解决指南
  • css实现渐进中嵌套渐进的方法
  • JavaWeb后端学习
  • VUE_TypeError: Cannot convert a BigInt value to a number at Math.pow 解决方法
  • Linux下mysql数据库的导入与导出以及查看端口
  • Open3d入门 一文读懂三维点云
  • pyinstaller系列教程(一)-基础介绍
  • echarts图表:类目轴