Swagger接口文档的使用
1、引入依赖
在POM文件添加swagger-spring-boot-starter依赖
<!-- Spring Boot 集成 swagger -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
2、配置Swagger
在 bootstrap.yml中配置swagger的扫描包路径及其它信息,base-package为扫描的包路径,扫描Controller类。
swagger:
title: ""#标题
description: "描述"
base-package: #扫描包路径
enabled: true #是否开启swagger
version: 1.0.0 #版本信息
3、在启动类中添加@EnableSwagger2Doc注解
@EnableSwagger2Doc
@SpringBootApplication
public class GulimallCouponApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallCouponApplication.class, args);
}
}
4、Swagger接口文档的注解
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
5、启动服务访问接口文档网站
http://localhost:端口号/路径/swagger-ui.html