[swagger]swagger接口规范学习笔记

来源:互联网 发布:netflix ribbon源码 编辑:程序博客网 时间:2024/05/29 14:16

1. bean对象中添加注解

1.1 class上添加注解@ApiModel
1.2 属性上添加注解@ApiModelProperty(value = “姓名”, example = “name”),属性是属于对象关联属性则不需要添加example 。

@ApiModel(value = "请求结果")public class ApiResult<T> {    @ApiModelProperty(value = "请求状态代码1为成功,其他为异常")    private int statusCode = FAIL;    @ApiModelProperty(value = "请求返回结果信息")    private String message;    @ApiModelProperty(value = "请求结果数据集")    private T data = null;}

2. controller中添加注解

2.1 class上添加注解

@Api(description = ” swagger 事例”) ,description可以描述这个controller 是用来做什么的;
@ApiIgnore:在class 上是过滤掉这个controller 不让这个类下面的接口在前端显示,在方法上让这个接口不在前端显示。

2.2 方法上添加注解

  • @ApiOperation(value = “test”,notes = “test”,produces = “application/json”)
    说明:
    value:方法名
    notes:方法描述
    produces:相应格式(统一为application/json)
  • @ApiImplicitParams({
    @ApiImplicitParam(name = “subcategoryId”, value = “年级iD”, required = true, paramType = “query”, dataType = “string”)
    })
    说明:参数传入每一个@ApiImplicitParam表示一个参数
    name:参数名,通过request.getParameter(“name”).的名字
    value:说明
    required :是否必填,true:必填,false :不必填
    paramType :参数获取类型(统一使用query )
    dataType :数据类型
@Api(value = "swagger", description = "接口描述", position = 2)public class TestController {   @ApiOperation(value = "test",notes = "test",produces = "application/json")     public ApiResult test();   @ApiImplicitParams( value = {      @ApiImplicitParam(paramType = "path",name = "str", value = "描述", required = true, dataType = "String"),   })   public ApiResult test1(String str);}

3. 访问swaggerUI

只需通过在工程访问路径后加上swagger-ui.html