阅读量:308
要在Spring Boot项目中结合使用Swagger,你需要遵循以下步骤:
添加依赖
在你的pom.xml文件中添加以下依赖:
<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>
请注意,你可能需要根据你的项目需求选择合适的版本。
配置Swagger
创建一个新的Java类,例如SwaggerConfig.java,并添加以下代码:
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("your.base.package"))
.paths(PathSelectors.any())
.build();
}
}
将your.base.package替换为你的Spring Boot项目中控制器类的包名。
访问Swagger UI
启动你的Spring Boot应用程序,然后在浏览器中访问以下URL:
http://localhost:8080/swagger-ui.html
你应该能看到Swagger UI界面,其中列出了你的项目中所有可用的API。