Swagger2的使用

使用Swagger2

1、依赖

1
2
3
4
5
6
7
8
9
10
<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、配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.controller"))
.build();
}
public ApiInfo apiInfo(){
Contact contact = new Contact("段友元", "----------", "17371584524@163.com");
return new ApiInfo(
"UserApi", "用户管理接口", "1.0", "urn:tos",
contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>()
);
}
}

3、使用

1
2
3
4
5
6
7
8
@ApiModel("用户实体类")
//注解在Entity类上
@ApiModelProperty("姓名")
//注解在Entity类字段上
@Api(tags = "用户管理")
//注解在Controller类上
@ApiOperation(value = "查询全部")
//注解在Controller类API上