Spring Boot2集成Swagger框架
要在Spring Boot 2中集成Swagger,你需要按照以下步骤操作:
- 在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配置类:
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.any())
.paths(PathSelectors.any())
.build();
}
}
- 在Spring Boot应用的主类或者配置类上添加
@EnableSwagger2
注解。 - 启动Spring Boot应用,并访问
http://<host>:<port>/swagger-ui.html
来查看Swagger文档。
以上步骤将会在你的Spring Boot应用中集成Swagger,并允许你通过Swagger UI来查看和测试REST API。
评论已关闭