本文介绍如何在linux环境下配置swagger,主要涵盖swagger ui和swagger editor的安装与配置。
一、 使用docker容器部署 (推荐)
此方法简化了安装过程,避免了依赖管理的复杂性。
-
安装docker: 若未安装docker,请执行以下命令:
sudo apt-get update sudo apt-get install -y docker.io sudo systemctl start docker sudo systemctl enable docker
登录后复制 -
拉取镜像: 分别拉取swagger ui和swagger editor镜像:
docker pull swaggerapi/swagger-ui:latest docker pull swaggerapi/swagger-editor:latest
登录后复制(使用 latest 标签获取最新版本)
-
运行容器: 运行swagger ui和swagger editor容器,并映射端口:
docker run -d -p 8080:8080 swaggerapi/swagger-ui:latest docker run -d -p 8081:8080 swaggerapi/swagger-editor:latest
登录后复制访问 http://:8080 查看swagger ui,访问 http://:8081 查看swagger editor。
二、 使用npm安装 (适用于需要更精细控制的情况)
此方法需要预先安装node.js和npm。
-
安装node.js和npm: 使用以下命令安装:
curl -sl https://deb.nodesource.com/setup_16.x | sudo -e bash - # 使用16.x版本或其他稳定版本 sudo apt-get install -y nodejs
登录后复制 -
安装依赖: 安装express和http-server:
npm install -g express http-server
登录后复制 -
安装swagger ui:
git clone https://github.com/swagger-api/swagger-ui.git cd swagger-ui npm install http-server -p 8080
登录后复制 -
安装swagger editor:
git clone https://github.com/swagger-api/swagger-editor.git cd swagger-editor npm install http-server -p 8081
登录后复制同样,访问 http://:8080 查看swagger ui,访问 http://:8081 查看swagger editor。
三、 spring boot集成swagger (适用于spring boot项目)
对于spring boot项目,推荐使用springdoc openapi来集成swagger。
-
添加依赖: 在 pom.xml 文件中添加依赖:
<dependency> <groupid>org.springdoc</groupid> <artifactid>springdoc-openapi-starter-webmvc-ui</artifactid> <version>2.1.0</version> </dependency>
登录后复制 -
配置swagger (可选): 可以创建一个配置类来自定义swagger文档信息:
import org.springdoc.core.models.groupedopenapi; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @configuration public class swaggerconfig { @bean public groupedopenapi publicapi() { return groupedopenapi.builder() .group("public") .pathstomatch("/public/**") .build(); } }
登录后复制 -
启动应用: 启动你的spring boot应用,访问 http://:8080/swagger-ui/index.html 查看swagger文档 (端口可能因应用配置而异)。
通过以上方法,您可以在linux环境下轻松配置swagger,方便api文档的管理和测试。 记住替换 为你的实际服务器ip地址。
以上就是swagger在linux环境下如何配置的详细内容,更多请关注代码网其它相关文章!
发表评论