在Ubuntu上生成Swagger文档通常涉及以下几个步骤:
首先,确保你的系统上已经安装了Node.js和npm。你可以通过以下命令来安装它们:
sudo apt update
sudo apt install nodejs npm
或者,你可以从Node.js官网下载并安装最新版本的Node.js和npm。
有几种方法可以在Ubuntu上安装Swagger UI:
sudo npm install -g http-server
然后,你可以通过以下命令在任意目录启动Swagger UI:
http-server
默认情况下,Swagger UI会在http://localhost:8080
上运行。
你可以从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
如果你正在使用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();
}
}
启动你的Spring Boot应用后,你可以通过以下URL访问Swagger文档:
http://localhost:8080/swagger-ui.html
如果你使用的是springdoc
,则访问:
http://localhost:8080/docs
如果你需要更强大的API文档编辑功能,可以安装Swagger Editor。你可以从Swagger Editor的GitHub仓库下载并安装它。安装完成后,你可以通过以下命令启动Swagger Editor:
npm start
然后在浏览器中访问http://localhost:9000
来使用Swagger Editor编辑和查看你的API文档。
以上就是在Ubuntu上生成Swagger文档的基本步骤。希望这些信息对你有所帮助!