在Linux系统中集成Swagger与Kubernetes可以通过以下步骤实现:
docker pull swaggerapi/swagger-ui:v4.6.0
docker run -d -p 38080:8080 swaggerapi/swagger-ui:v4.6.0
http://<your-k8s-node-ip>:38080
,即可看到Swagger UI界面,其中列出了你的所有API端点。docker pull swaggerapi/swagger-editor:v4.6.0
docker run -d -p 38081:8080 swaggerapi/swagger-editor:v4.6.0
http://<your-k8s-node-ip>:38081
,即可使用Swagger Editor在线编辑和测试API文档。在 pom.xml
文件中添加Swagger依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
创建一个Swagger配置类来启用Swagger文档生成:
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应用程序后,Swagger会自动生成API文档,并且你可以在浏览器中访问 http://<your-k8s-node-ip>:8080/swagger-ui.html
来查看和测试API文档。
通过以上步骤,你可以在Linux系统中成功集成Swagger与Kubernetes,实现API文档的自动生成、查看和测试功能。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: linux怎么查看版本信息