swagger常用注解说明

来源:互联网 发布:apache tomcat怎么用 编辑:程序博客网 时间:2024/05/22 11:58
常用到的注解有:
  • Api
  • ApiModel
  • ApiModelProperty
  • ApiOperation
  • ApiParam
  • ApiResponse
  • ApiResponses
  • ResponseHeader
1. api标记

Api 用在类上,说明该类的作用。可以标记一个Controller类做为swagger 文档资源,使用方式:

@Api(value = "/user", description = "Operations about user")

与Controller注解并列使用。 属性配置:

属性名称备注valueurl的路径值tags如果设置这个值、value的值会被覆盖description对api资源的描述basePath基本路径可以不配置position如果配置多个Api 想改变显示的顺序位置producesFor example, "application/json, application/xml"consumesFor example, "application/json, application/xml"protocolsPossible values: http, https, ws, wss.authorizations高级特性认证时配置hidden配置为true 将在文档中隐藏

在SpringMvc中的配置如下:

@Controller@RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})@Api(value = "/pet", description = "Operations about pets")public class PetController {}
2. ApiOperation标记

ApiOperation:用在方法上,说明方法的作用,每一个url资源的定义,使用方式:

@ApiOperation(          value = "Find purchase order by ID",          notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",          response = Order,          tags = {"Pet Store"})

与Controller中的方法并列使用。
属性配置:

属性名称备注valueurl的路径值tags如果设置这个值、value的值会被覆盖description对api资源的描述basePath基本路径可以不配置position如果配置多个Api 想改变显示的顺序位置producesFor example, "application/json, application/xml"consumesFor example, "application/json, application/xml"protocolsPossible values: http, https, ws, wss.authorizations高级特性认证时配置hidden配置为true 将在文档中隐藏response返回的对象responseContainer这些对象是有效的 "List", "Set" or "Map".,其他无效httpMethod"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH"codehttp的状态码 默认 200extensions扩展属性

在SpringMvc中的配置如下:

@RequestMapping(value = "/order/{orderId}", method = GET)  @ApiOperation(      value = "Find purchase order by ID",      notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",      response = Order.class,      tags = { "Pet Store" })   public ResponseEntity<Order> getOrderById(@PathVariable("orderId") String orderId)      throws NotFoundException {    Order order = storeData.get(Long.valueOf(orderId));    if (null != order) {      return ok(order);    } else {      throw new NotFoundException(404, "Order not found");    }  }
3. ApiParam标记

ApiParam请求属性,使用方式:

public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true)  User user)

与Controller中的方法并列使用。

属性配置:

属性名称备注name属性名称value属性值defaultValue默认属性值allowableValues可以不配置required是否属性必填access不过多描述allowMultiple默认为falsehidden隐藏该属性example举例子

在SpringMvc中的配置如下:

 public ResponseEntity<Order> getOrderById(      @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)      @PathVariable("orderId") String orderId)
4. ApiResponse

ApiResponse:响应配置,使用方式:

@ApiResponse(code = 400, message = "Invalid user supplied")

与Controller中的方法并列使用。 属性配置:

属性名称备注codehttp的状态码message描述response默认响应类 Voidreference参考ApiOperation中配置responseHeaders参考 ResponseHeader 属性配置说明responseContainer参考ApiOperation中配置

在SpringMvc中的配置如下:

 @RequestMapping(value = "/order", method = POST)  @ApiOperation(value = "Place an order for a pet", response = Order.class)  @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })  public ResponseEntity<String> placeOrder(      @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {    storeData.add(order);    return ok("");  }
5. ApiResponses

ApiResponses:响应集配置,使用方式:

 @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

与Controller中的方法并列使用。 属性配置:

属性名称备注value多个ApiResponse配置

在SpringMvc中的配置如下:

 @RequestMapping(value = "/order", method = POST)  @ApiOperation(value = "Place an order for a pet", response = Order.class)  @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })  public ResponseEntity<String> placeOrder(      @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {    storeData.add(order);    return ok("");  }
6. ResponseHeader

响应头设置,使用方法

@ResponseHeader(name="head1",description="response head conf")

与Controller中的方法并列使用。 属性配置:

属性名称备注name响应头名称description头描述response默认响应类 VoidresponseContainer参考ApiOperation中配置

在SpringMvc中的配置如下:

@ApiModel(description = "群组")
7. 其他
  • @ApiImplicitParams:用在方法上包含一组参数说明;
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方
    • name:参数代表的含义
    • value:参数名称
    • dataType: 参数类型,有String/int,无用
    • required : 是否必要
    • defaultValue:参数的默认值
  • @ApiResponses:用于表示一组响应;
  • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息;
    • code: 响应码(int型),可自定义
    • message:状态码对应的响应信息
  • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候;
  • @ApiModelProperty:描述一个model的属性。


作者:Xiangdong_She
链接:https://www.jianshu.com/p/12f4394462d5
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 描写山的诗词 山语世家 山语豪庭 南宁山语城 山语城二手房 烟台山语城二手房 永意山语城 保亭蓝城山语泉 逸合山语城 长沙山语城 烟台山语城 山语城房价 长沙山语城房价 江湾山语城 山语城户型图 凤岭山语城 山语海为什么那么便宜 山语海 海会 实力山语间 重庆山语间 青城山语间别墅 山语间 山谷风的形成原理 山豆根 山豆根图片 山豆根超高清图片 山豆根中毒 山豆根价格 山豆根种子 山豆根收一斤多少钱 山豆根怎么情况下才中毒 山豆根的功效与作用 山财大 山财大教务系统 上海财大在职研究生 山贼王我当定了 陶渊明独爱菊 山边灯 车边灯 挂车边灯 货车边灯