在Linux环境下配置Swagger,通常是指安装和配置Swagger UI和Swagger Editor。以下是详细的步骤:
安装Swagger UI和Swagger Editor
使用Docker容器部署
-
安装Docker(如果尚未安装):
sudo apt-get update sudo apt-get install -y docker.io sudo systemctl start docker sudo systemctl enable docker -
拉取Swagger UI镜像:
docker pull swaggerapi/swagger-ui:v4.6.0 -
拉取Swagger Editor镜像:
docker pull swaggerapi/swagger-editor:v4.6.0 -
运行Swagger UI容器:
docker run -d -p 38080:8080 swaggerapi/swagger-ui:v4.6.0 -
运行Swagger Editor容器:
docker run -d -p 38081:8080 swaggerapi/swagger-editor:v4.6.0现在,你可以通过访问
http://来查看Swagger UI,通过访问:38080 http://来查看Swagger Editor。:38081
使用npm安装
-
安装Node.js和npm(如果尚未安装):
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs -
安装Express:
npm install -g express -
安装http-server:
npm install -g http-server -
下载并解压Swagger UI:
git clone https://github.com/swagger-api/swagger-ui.git cd swagger-ui npm install -
启动Swagger UI:
http-server -p 8080 -
下载并解压Swagger Editor:
git clone https://github.com/swagger-api/swagger-editor.git cd swagger-editor npm install -
启动Swagger Editor:
http-server -p 8081现在,你可以通过访问
http://来查看Swagger UI,通过访问:8080 http://来查看Swagger Editor。:8081
配置Swagger
使用Spring Boot集成Swagger(适用于Spring Boot项目)
-
添加依赖: 在
pom.xml中添加以下依赖:<dependency> <groupId>org.springdocgroupId> <artifactId>springdoc-openapi-starter-webmvc-uiartifactId> <version>2.1.0version> dependency> -
配置Swagger: 创建一个配置类:
import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.info.Info; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @OpenAPIDefinition(info = @Info(title = "My API", version = "1.0")) public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } -
启动应用: 启动你的Spring Boot应用,然后访问
http://查看Swagger文档。:8080/swagger-ui/
通过以上步骤,你可以在Linux环境下成功配置和运行Swagger UI和Swagger Editor。
以上就是关于“Swagger在Linux环境下如何配置”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm