# SpringBoot整合Swagger2

## 添加依赖包

```
<!--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>
```

## 编写配置文件

```
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.luchong.report.report_spring_boot.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建文档的详细信息函数
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("学生组织系统")
                .contact(new Contact("chong.lu", "https://luchong.gitbook.io/", "luchong1999@outlook.com"))
                .version("v1").description("初探SpringBoot")
                .build();
    }

}
```

## 注解使用

### @Api 修饰类

```
@Api(tags = "志愿信息")
public class ReportController {
```

tags：说明该类的作用，可以在UI界面上看到的注解,value=该参数没什么意义、在UI界面上也看到

### @ApiOperation 方法描述

```
@ApiOperation(value = "志愿信息",notes = "获取所有志愿信息")
```

### @ApiResponses、@ApiResponse 修饰返回信息

```
@ApiResponses({@ApiResponse(code = 1,message = "操作成功")})
```
