使用 Springfox Swagger2 插件进行集成swagger
时间: 2023-11-27 19:50:58 浏览: 176
Swagger是一个流行的API文档工具,Springfox Swagger2可以帮助我们将Swagger集成到Spring Boot应用程序中。下面是使用Springfox Swagger2插件进行集成Swagger的步骤:
1.在pom.xml文件中添加以下依赖:
```
<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>
```
2.创建Swagger配置类
创建一个配置类来配置Swagger,如下所示:
```
@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();
}
}
```
3.启动应用程序并访问Swagger UI
启动应用程序后,访问 http://localhost:8080/swagger-ui.html 可以查看Swagger UI。在Swagger UI中,您可以查看所有已公开的接口,测试它们并查看其文档。
值得注意的是,上述Swagger配置类中的配置只是最基本的配置,您可以根据自己的需要进行更高级的配置,例如添加描述信息,定义全局响应消息等。
阅读全文
相关推荐
















