swagger2生成api接口文档
时间: 2023-08-29 07:12:46 浏览: 107
Swagger是一种RESTful API文档生成工具,可以方便地生成API接口文档,提高API接口的可读性和可维护性。下面是使用Swagger2生成API接口文档的步骤:
1.添加Swagger2依赖
在pom.xml文件中添加以下依赖:
```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.创建Swagger2配置类
创建一个Swagger2配置类,用于配置Swagger2的各种参数,可以参考以下示例:
```java
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API接口文档")
.description("API接口文档")
.version("1.0")
.build();
}
}
```
3.启动项目并访问Swagger UI
启动项目后,在浏览器中输入以下地址即可访问Swagger UI:
```
http://localhost:8080/swagger-ui.html
```
在Swagger UI界面中,可以查看所有生成的API接口文档,并且可以测试API接口。