温馨提示×

Ubuntu如何搭建Swagger环境

小樊
44
2025-03-26 08:41:49
栏目: 智能运维
Ubuntu服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Ubuntu上搭建Swagger环境可以通过以下步骤完成:

1. 安装Node.js和npm

首先,确保你的系统上已经安装了Node.js和npm。你可以通过以下命令来安装它们:

sudo apt update
sudo apt install -y nodejs npm

验证安装是否成功:

node -v
npm -v

2. 安装Swagger Editor

你可以通过npm全局安装Swagger Editor:

sudo npm install -g http-server

然后,下载并解压Swagger Editor:

wget https://github.com/swagger-api/swagger-editor/archive/refs/tags/v3.16.1.tar.gz
tar -xvf v3.16.1.tar.gz
cd swagger-editor-3.16.1

启动Swagger Editor:

http-server -p 8080

访问http://localhost:8080来使用Swagger Editor。

3. 安装Swagger UI

同样,你可以通过npm全局安装Swagger UI:

sudo npm install -g swagger-ui

然后,下载并解压Swagger UI:

wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v3.48.0.tar.gz
tar -xvf v3.48.0.tar.gz
cd swagger-ui-3.48.0

启动Swagger UI:

npm install
node index.js

访问http://localhost:3000来使用Swagger UI。

4. 使用Springdoc(适用于Spring Boot项目)

如果你使用的是Spring Boot项目,推荐使用springdoc,它是一个自动生成API文档的工具,集成Swagger UI和Redoc。

添加以下依赖到你的pom.xml

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.1.0</version>
</dependency>

然后在你的主类上添加@EnableSpringDoc注解:

import io.swagger.v3.oas.annotations.EnableSpringDoc;

@SpringBootApplication
@EnableSpringDoc
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

启动你的Spring Boot应用,然后访问http://localhost:8080/swagger-ui/来查看自动生成的API文档。

以上步骤应该能帮助你在Ubuntu上成功搭建Swagger环境。如果你遇到任何问题,请检查网络连接和防火墙设置,并确保所有依赖项都已正确安装。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:如何用Ubuntu搭建Swagger服务

0