在CentOS系统上将Swagger集成到Spring Boot项目中,可以按照以下步骤进行:
1. 创建Spring Boot项目
首先,你需要创建一个Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。
2. 添加Swagger依赖
在你的pom.xml文件中添加Swagger和Springfox的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<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>
3. 配置Swagger
创建一个配置类来配置Swagger:
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.basePackage("com.example.demo.controller")) // 替换为你的控制器包路径
.paths(PathSelectors.any())
.build();
}
}
4. 启动应用
在你的Spring Boot应用的主类上添加@SpringBootApplication注解,并启动应用:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5. 访问Swagger UI
启动应用后,打开浏览器并访问以下URL来查看Swagger UI:
http://localhost:8080/swagger-ui.html
你应该能够看到Swagger UI界面,其中列出了你的API文档。
6. 添加API文档
在你的控制器类中添加Swagger注解来描述你的API:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@Api(tags = "示例控制器")
public class ExampleController {
@GetMapping("/hello")
@ApiOperation("返回一个简单的问候")
public String sayHello() {
return "Hello, World!";
}
}
7. 重新启动应用
保存所有更改并重新启动你的Spring Boot应用。然后再次访问Swagger UI页面,你应该能够看到更新后的API文档。
通过以上步骤,你就可以在CentOS系统上成功地将Swagger集成到Spring Boot项目中。
以上就是关于“centos swagger集成Spring Boot”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm