在Linux系统上使用Swagger导出API文档,可以按照以下步骤进行:
npm install -g swagger-jsdoc
docker pull swaggerapi/swagger-editor
docker run -d -p 8080:8080 swaggerapi/swagger-editor
docker pull swaggerapi/swagger-ui
docker run -d -p 8081:8081 swaggerapi/swagger-ui
在你的项目根目录下创建一个名为swagger.yaml
的文件,用于定义API文档的结构。
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
description: API documentation for my application
paths:
/users:
get:
summary: List all users
responses:
'200':
description: A JSON array of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
required:
- id
- name
使用swagger-jsdoc
工具从你的API规范文件生成文档。
swagger-jsdoc -c path/to/swagger.yaml
启动你的Spring Boot项目后,访问http://localhost:8080/swagger-ui.html
,你将看到Swagger UI页面,其中包含了你的API文档。在Swagger UI页面中,点击“Authorize”按钮,然后点击“Download Swagger JSON”按钮以下载一个包含所有API信息的JSON文件。你也可以选择导出为YAML格式,只需点击“Download Swagger YAML”按钮即可。
你可以使用Swagger Editor在线编辑和验证你的OpenAPI规范文件(YAML或JSON格式)。
你可以将Swagger文档导入Postman、SoapUI等工具,这些工具将会为你自动创建自动化测试。
通过以上步骤,你就可以在Linux系统上使用Swagger生成交互式API文档,并将其导出为不同格式的文档文件,方便项目成员之间的沟通与协作。