如何配置swagger2
时间: 2023-11-16 10:46:30 浏览: 49
Swagger2是一个用于生成API文档的工具,需要进行以下配置:
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. 配置Swagger2:创建Swagger2配置类,使用@EnableSwagger2注解开启Swagger2
```
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger2 Demo")
.description("Swagger2 Demo API Documentation")
.version("1.0")
.build();
}
}
```
其中,docket()方法配置了API文档的基本信息和扫描的包路径,apiInfo()方法配置了API文档的标题、描述和版本号等信息。
3. 使用Swagger2:启动项目,访问`http://localhost:port/swagger-ui.html`即可查看API文档。
以上就是配置Swagger2的步骤。需要注意的是,在实际开发中,需要根据具体的业务场景进行配置,例如添加鉴权、禁用某些接口等。
阅读全文
相关推荐


















