swagger2配置及依賴
时间: 2024-03-12 22:39:14 浏览: 22
Swagger2 是一个用于生成和描述 RESTful API 的工具,它可以自动生成 API 文档,并提供了一个可交互的 UI 界面,方便开发者测试 API 接口。在使用 Swagger2 之前,需要进行以下配置和依赖的添加:
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. 在 Spring Boot 启动类上添加 `@EnableSwagger2` 注解开启 Swagger2:
```
@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
3. 在配置类中添加 `Docket` Bean 对象,用于配置 Swagger2 的一些基本信息:
```
@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();
}
}
```
以上就是 Swagger2 的配置和依赖添加的步骤。