要在CentOS上将Swagger与Spring Boot集成,你需要遵循以下步骤:
- 添加依赖:在你的Spring Boot项目的
pom.xml文件中添加Swagger和Swagger UI的依赖。
<dependencies>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.9.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.9.2version>
dependency>
dependencies>
请注意,版本号可能会随着时间而变化,所以请检查最新的版本。
- 配置Swagger:创建一个配置类来定义Swagger的API文档。
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.basePackage("com.yourpackage")) // 替换为你的控制器包路径
.paths(PathSelectors.any())
.build();
}
}
-
访问Swagger UI:启动你的Spring Boot应用程序后,你可以通过浏览器访问Swagger UI界面。默认情况下,URL应该是
http://。: /swagger-ui.html -
(可选)自定义Swagger UI:如果你想要自定义Swagger UI的外观或者行为,你可以修改
SwaggerConfig类中的配置,或者添加额外的配置参数。 -
运行和测试:确保你的Spring Boot应用程序正在运行,并且你可以通过浏览器访问Swagger UI界面来查看和测试你的API文档。
这些步骤应该可以帮助你在CentOS上运行的Spring Boot应用程序中集成Swagger。记得在部署到生产环境之前移除或禁用Swagger,因为它可能会暴露敏感信息。
以上就是关于“Centos Swagger与Spring Boot如何集成”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm