在 Linux 下使用 Swagger,通常是指 Swagger/OpenAPI 用于 API 文档的编写、查看和测试。下面按最常见场景给你一个清晰、实用的说明。
Swagger 是一套 RESTful API 文档工具,现在主流标准是 OpenAPI。
常见用途:
适合:查看和调试 API
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.14.zip
unzip v5.17.14.zip
cd swagger-ui-5.17.14/dist
python3 -m http.server 8080
http://服务器IP:8080
修改 swagger-initializer.js:
url: "http://你的IP:端口/v3/api-docs",
或者直接在页面顶部输入:
http://localhost:8080/v3/api-docs
✅ 适合 Java / Spring Boot / Go / Node 等后端项目
适合 Java 后端
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
访问:
http://localhost:8080/swagger-ui.html
或:
http://localhost:8080/swagger-ui/index.html
✅ 无需额外安装,Linux 服务器直接访问即可
适合生成、校验 API 文档
npm install -g @apidevtools/swagger-cli
(如果没有 npm)
sudo apt install nodejs npm
swagger-cli validate openapi.yaml
swagger-cli bundle openapi.yaml -o bundled.yaml
openapi: 3.0.3
info:
title: Linux API Demo
version: 1.0.0
paths:
/hello:
get:
summary: Hello接口
responses:
'200':
description: OK
✅ 使用:
curl http://localhost:8080/v3/api-docs
| 名称 | 说明 |
|---|---|
| Swagger | 老品牌 |
| OpenAPI | 官方标准 |
| Swagger UI | 可视化工具 |
✅ 现在一般用 OpenAPI 3.x + Swagger UI
| 你是… | 推荐方式 |
|---|---|
| Java 后端 | Spring Boot + springdoc |
| 只想看接口 | Swagger UI |
| 写 API 文档 | openapi.yaml |
| 自动生成代码 | Swagger Codegen |
如果你愿意,可以告诉我:
我可以直接给你 完整可运行的示例。