在Debian系统上将Swagger与Spring Boot结合使用,可以按照以下步骤进行:
-
安装Java和Maven: 确保你的Debian系统上已经安装了Java和Maven。如果没有安装,可以使用以下命令进行安装:
sudo apt update sudo apt install openjdk-11-jdk maven -
创建Spring Boot项目: 你可以使用Spring Initializr(https://start.spring.io/)来生成一个Spring Boot项目。选择所需的依赖项,例如Spring Web。
或者,你也可以手动创建一个Spring Boot项目,并在
pom.xml文件中添加Swagger依赖项。 -
添加Swagger依赖: 在你的Spring Boot项目的
pom.xml文件中添加Swagger依赖项。以下是Swagger 2的依赖项示例:<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>如果你使用的是Spring Boot 2.6及以上版本,可能需要使用Swagger 3(Springfox 3.0.0),其依赖项如下:
<dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-boot-starterartifactId> <version>3.0.0version> dependency> -
配置Swagger: 创建一个配置类来配置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; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } }请将
com.example.demo替换为你的Spring Boot项目的包名。 -
启动Spring Boot应用程序: 使用以下命令启动你的Spring Boot应用程序:
mvn spring-boot:run -
访问Swagger UI: 打开浏览器并访问以下URL来查看Swagger UI:
http://localhost:8080/swagger-ui.html你应该能够看到Swagger UI界面,其中列出了你的API文档。
通过以上步骤,你就可以在Debian系统上成功地将Swagger与Spring Boot结合使用了。
以上就是关于“在Debian上Swagger与Spring Boot如何结合”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm