在Ubuntu上生成Swagger文档通常涉及以下几个步骤:
1. 安装Node.js和npm
首先,确保你的系统上已经安装了Node.js和npm。你可以通过以下命令来安装它们:
sudo apt update
sudo apt install nodejs npm
或者,你可以从Node.js官网下载并安装最新版本的Node.js和npm。
2. 安装Swagger UI
有几种方法可以在Ubuntu上安装Swagger UI:
方法一:从npm全局安装http-server
sudo npm install -g http-server
然后,你可以通过以下命令在任意目录启动Swagger UI:
http-server
默认情况下,Swagger UI会在http://localhost:8080上运行。
方法二:从源码安装Swagger UI
你可以从Swagger UI的GitHub仓库下载最新版本的Swagger UI源码,然后解压并在dist目录下运行http-server。
git clone https://github.com/swagger-api/swagger-ui.git
cd swagger-ui
npm install
http-server dist
3. 配置Swagger
如果你正在使用Spring Boot项目,你可以使用springdoc来自动生成Swagger文档。首先,添加springdoc-openapi-starter-webmvc-ui依赖到你的pom.xml文件中:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
然后,在Spring Boot应用的主类上添加@EnableSwagger2注解:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
4. 访问Swagger文档
启动你的Spring Boot应用后,你可以通过以下URL访问Swagger文档:
http://localhost:8080/swagger-ui.html
如果你使用的是springdoc,则访问:
http://localhost:8080/docs
5. 使用Swagger Editor(可选)
如果你需要更强大的API文档编辑功能,可以安装Swagger Editor。你可以从Swagger Editor的GitHub仓库下载并安装它。安装完成后,你可以通过以下命令启动Swagger Editor:
npm start
然后在浏览器中访问http://localhost:9000来使用Swagger Editor编辑和查看你的API文档。
以上就是在Ubuntu上生成Swagger文档的基本步骤。希望这些信息对你有所帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1281205.html