运用swagger编写api文档

一.什么是swagger

随着互联网技术的发展,前后端技术在各自的道路上越走越远,他们之间的唯一联系变成了api接口,api接口文档编程了前后端人员的纽带,而swagger就是书写api文档的一款框架.

官网: https://swagger.io/

相关资源下载地址: https://download.csdn.net/download/zhixingwu/12008773

推荐: 也可以使用 showdoc来书写api文档. 官网: https://www.showdoc.cc/

二.SwaggerEditor的安装与启动

  1. 下载 swagger-editor.zip
  2. 解压swagger-editor
  3. 全局安装http-server(http-server是一个简单的零配置命令行http服务器)

    npm install ‐g http‐server

  4. 启动swagger-editor

    http‐server swagger‐editor

  5. 浏览器打开: http://localhost:8080

    三.语法规则

    1.固定字段

    字段名 类型 描述
    swagger string 必需的。使用指定的规范版本
    info info Object 必需的。提供元数据API
    host string 主机名或ip服务API
    basePath string API的基本路径
    schemes [string] API的传输协议。 值必须从列表中:"http","https","ws","wss"。
    consumes [string] 一个MIME类型的api可以使用列表。值必须是所描述的Mime类型
    produces [string] MIME类型的api可以产生的列表。 值必须是所描述的Mime类型
    paths 路径对象 必需的。可用的路径和操作的API
    definitions 定义对象 一个对象数据类型生产和使用操作
    parameters 参数定义对象 一个对象来保存参数,可以使用在操作。 这个属性不为所有操作定义全局参数。
    responses 反应定义对象 一个对象响应,可以跨操作使用。 这个属性不为所有操作定义全球响应
    externalDocs 外部文档对象 额外的外部文档
    summary string 什么操作的一个简短的总结。 最大swagger-ui可读性,这一领域应小于120个字符
    description string 解释操作的行为
    operationId string 独特的字符串用于识别操作。 id必须是唯一的在所有业务中所描述的API。 工具和库可以使用operationId来唯一地标识一个操作,因此,建议遵循通用的编程的命名约定
    deprecated boolean 声明该操作被弃用。 使用声明的操作应该没有。 默认值是false

2.字段类型与格式定义

普通名字 type format 说明
integer integer int32 签署了32位
long integer int64 签署了64位
float number float
double number double
string string
byte string byte base64编码的字符
binary stirng binary 任何的八位字节序列
boolean boolean
date string date 所定义的full-date- - - - - -RFC3339
datetime string date-time 所定义的date-time- - - - - -RFC3339
password stirng password 用来提示用户界面输入需要模糊

四.示例-城市API文档

swagger: '2.0'
info:
  version: "1.0.0"
  title: 基础模块-城市API
host: api.pcloud.com
basePath: /base
paths:
  /city:
    post:
      summary: 新增城市
      parameters:
        -
          name: body
          in: body
          description: 城市实体类
          required: true
          schema:
            $ref: '#/definitions/City'
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiResponse'
    get:
      summary: 返回城市列表
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiCityListResponse'
  /city/{cityId}:
    put:
      summary: 修改城市
      parameters:
        - name: cityId
          in: path
          description: 城市ID
          required: true
          type: string
        - name: body
          in: body
          description: 城市实体类
          required: true
          schema:
            $ref: '#/definitions/City'
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiResponse'
    delete:
      summary: 根据ID删除城市
      parameters:
        - name: cityId
          in: path
          description: 城市ID
          required: true
          type: string
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiResponse'
    get:
      summary: 根据ID查询城市
      parameters:
        - name: cityId
          in: path
          description: 城市ID
          required: true
          type: string
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiCityResponse'
  /city/search:
    post:
      summary: 根据条件查询城市列表
      parameters:
        - name: body
          in: body
          description: 城市实体类
          required: true
          schema:
            $ref: '#/definitions/City'
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiCityListResponse'
  /city/search/{page}/{size}:
    post:
      summary: 城市分页列表
      parameters:
        - name: page
          in: path
          description: 页码
          required: true
          type: integer
          format: int32
        - name: size
          in: path
          description: 页大小
          required: true
          type: integer
          format: int32
        - name: body
          in: body
          description: 城市实体类
          required: true
          schema:
            $ref: '#/definitions/City'
      responses:
        200:
          description: 成功响应
          schema:
            $ref: '#/definitions/ApiCityPageResponse'
definitions:
  City:
    type: object
    properties:
      id:
        type: string
        description: ID
      name:
        type: string
        description: 城市名称
      ishot:
        type: string
        description: 是否热门
  ApiResponse:
    type: object
    properties:
      flag:
        type: boolean
        description: 是否成功
      code:
        type: integer
        format: int32
        description: 返回码
      message:
        type: string
        description: 返回信息
  ApiCityResponse:
    type: object
    properties:
      flag:
        type: boolean
        description: 是否成功
      code:
        type: integer
        format: int32
        description: 返回码
      message:
        type: string
        description: 返回信息
      data:
        $ref: '#/definitions/City'
  CityList:
    type: array
    items:
        $ref: '#/definitions/City'
  ApiCityListResponse:
    type: object
    properties:
      flag:
        type: boolean
        description: 是否成功
      code:
        type: integer
        format: int32
        description: 返回码
      message:
        type: string
        description: 返回信息
      data:
        $ref: '#/definitions/CityList'
  ApiCityPageResponse:
    type: object
    properties:
      flag:
        type: boolean
        description: 是否成功
      code:
        type: integer
        format: int32
        description: 返回码
      message:
        type: string
        description: 返回信息
      data:
        properties:
          total:
            type: integer
            format: int32
          rows:
            $ref: '#/definitions/CityList' 

五.批量生成API文档

使用<>自动生成所有标的yml文档,自动生成的文档中类型均为string ,我们这里需要再对类型进行修改即可.
步骤:
(1)执行建表脚本
(2)使用《黑马程序员代码生成器》生成脚本(HeimaCodeUtil_V2.4_64.zip)

六.swaggerUI

SwaggerUI是用来展示Swagger文档的界面,以下为安装步骤
(1)在本地安装nginx
(2)下载SwaggerUI源码 https://swagger.io/download-swagger-ui/
(3)解压,将dist文件夹下的全部文件拷贝至 nginx的html目录
(4)启动nginx
(5)浏览器打开页面 http://localhost即可看到文档页面
(6)将编写好的yml文件也拷贝至nginx的html目录,这样我们就可以加载我们的swagger文档了

原文地址:https://www.cnblogs.com/itzlg/p/11966754.html

时间: 2024-07-31 04:19:28

运用swagger编写api文档的相关文章

Asp.Net Core下使用swagger生成api文档

目录 一.前期准备 二.配置Swagger 三.参考 .Net Core中有两个集成NSwag的包,分别为Swashbuckle和NSwag.两者的配置大同小异.这里以NSwag为例. 一.前期准备 1.初始化asp.net core 测试项目 新建asp.net core项目,此处略过: 新建apicontroller,并编写测试代码: [Route("api/[controller]")] [ApiController] public class UserApiController

Core Web API上使用Swagger提供API文档

在ASP.NET Core Web API上使用Swagger提供API文档 我在开发自己的博客系统(http://daxnet.me)时,给自己的RESTful服务增加了基于Swagger的API文档功能.当设置IISExpress的默认启动路由到Swagger的API文档页面后,在IISExpress启动Web API站点后,会自动重定向到API文档页面,非常方便.这不仅让我能够快速省查API设计的合理性,同时从API的使用角度也为我自己提供了便捷.下图就是我的博客系统RESTful API

springboot利用swagger构建api文档

一.引入jar pom.xml <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.spr

浅析如何在Nancy中使用Swagger生成API文档

原文:浅析如何在Nancy中使用Swagger生成API文档 前言 上一篇博客介绍了使用Nancy框架内部的方法来创建了一个简单到不能再简单的Document.但是还有许许多多的不足. 为了能稍微完善一下这个Document,这篇引用了当前流行的Swagger,以及另一个开源的Nancy.Swagger项目来完成今天的任务! 注:Swagger是已经相对成熟的了,但Nancy(2.0.0-clinteastwood)和Nancy.Swagger(2.2.6-alpha)是基于目前的最新版本,但目

Laravel(PHP)使用Swagger生成API文档不完全指南 - 基本概念和环境搭建 - 简书

在PHPer中,很多人听说过Swagger,部分人知道Swagger是用来做API文档的,然而只有少数人真正知道怎么正确使用Swagger,因为PHP界和Swagger相关的资料实在是太少了.所以鄙人斗胆一试,希望能以本文帮助到大家了解Swagger,从此告别成天用Word.Markdown折腾API文档的日子. 什么是Swagger Swagger is a simple yet powerful representation of your RESTful API. With the lar

Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api

本文作者:小雷FansUnion-一个有创业和投资经验的资深程序员-全球最大中文IT社区CSDN知名博主-排名第119 实际项目中非常需要写文档,提高Java服务端和Web前端以及移动端的对接效率. 听说Swagger这个工具,还不错,就网上找了些资料,自己实践了下. 一:Swagger介绍 Swagger是当前最好用的Restful API文档生成的开源项目,通过swagger-spring项目 实现了与SpingMVC框架的无缝集成功能,方便生成spring restful风格的接口文档,

ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介

参考地址,官网:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.2&tabs=visual-studio 与https://www.jianshu.com/p/349e130e40d5 当一个WebApi完成之后,书写API文档是一件非常头疼的事,因为不仅要写得清楚,能让调用接口的人看懂,又是非常耗时耗力的一件事.在之前的一篇随笔中(

Web Api 2.0中使用Swagger生成Api文档的2个小Tips

当Web Api 2.0使用OAuth2授权时,如何在Swagger中添加Authorization请求头? Swagger说明文档支持手动调用Api, 但是当Api使用OAuth2授权时,由于没有地方可以输入授权Token, 导致响应结果一直是401没有授权. 解决方案: 在Swagger配置文件中,添加对请求头中Authorization的设置. 1. 在Api项目中添加一个新类AddAuthorizationHeader并实现IOperationFilter接口 public class

Django Rest Swagger生成api文档

关于swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. Swagger 可以生成客户端SDK代码用于各种不同的平台上的实现. Swagger 文件可以在许多不同的平台上从代码注释中自动生成. Swagger 有一个强大的社区,里面有许多强悍的贡献者. 下面就实战django rest swagger为drf生成api接口文档 环境 Python3.6 Dja