为什么要使用Swagger相关的注解
一些类或方法使用Swagger相关的注解会更方便读懂接口
-
@Api类注解,在控制器类添加此注解,可以对控制器类进行功能说明
@Api(value = "商品管理",tags = "提供商品的增删改查接口")
-
@ApiOperation方法注解:说明方法接口的作用
-
@ApiImplicitParams和 @ApiImplicitParam注解:说明接口方法的参数
@ApiOperation("商品的查询接口")
@ApiImplicitParams({
@ApiImplicitParam(dataType ="String" ,name ="id" ,value ="商品的id" ,required = true),
@ApiImplicitParam(dataType = "String", name="type", value = "商品的类型",required = false,defaultValue = "null"),
})
@GetMapping("/getById")
public void get(String id,String type){
}
- @ApiModel和 @ApiModelProperty 当接口参数和返回值为对象类型事,在实体类中添加注解说明,请求方法上就不需加@ApiOperation注解了
@ApiModel(value = "商品信息",description = "商品的属性信息")
public class Goods {
@ApiModelProperty(dataType = "String",value = "商品id",required = false)
private String id;
@ApiModelProperty(dataType = "String",value = "商品名称",required = true)
private String name;
@ApiModelProperty(dataType = "String",value = "商品价格",required = true)
private String price;
}
- @ApiIgnore接口方法注解,添加此注解的方法将不会生成接口文档中
swagger-ui插件
导入依赖
<!-- swagger-ui插件 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
访问地址:http://localhost:8080/doc.html