[关闭]
@awsekfozc 2018-01-02T09:33:03.000000Z 字数 3615 阅读 2894

swagger常用注解说明

swagger

Api

Api 标记可以标记一个Controller类做为swagger 文档资源,使用方式:
  1. @Api(value = "/user", description = "Operations about user")

与Controller注解并列使用。

属性配置:

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

Demo

  1. @RestController
  2. @Slf4j
  3. @Api(tags = {"爬虫任务接口"})
  4. @RequestMapping(value = "/sprider")
  5. public class SpriderTaskService {
  6. }

ApiModel

ApiModel标记一个实体做为文档资源,如请求对象

Demo:

  1. @Data
  2. @ApiModel(value="新增用户请求")
  3. public class UserAddRequest {
  4. }

ApiModelProperty

ApiModelProperty标记属性

Demo:

  1. @ApiModelProperty(value = "网站地址", required = true)
  2. private String url;

ApiOperation

ApiOperation每一个url资源的定义,使用方式:
  1. @ApiOperation(
  2. value = "Find purchase order by ID",
  3. notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
  4. response = Order,
  5. tags = {"Pet Store"})

与Controller中的方法并列使用。

属性配置:

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

Demo:

  1. @RequestMapping(value = "/order/{orderId}", method = GET)
  2. @ApiOperation(
  3. value = "获取订单",
  4. notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
  5. response = Order.class,
  6. tags = { "Pet Store" })
  7. public ResponseEntity<Order> getOrderById(@PathVariable("orderId") String orderId)
  8. throws NotFoundException {
  9. Order order = storeData.get(Long.valueOf(orderId));
  10. if (null != order) {
  11. return ok(order);
  12. } else {
  13. throw new NotFoundException(404, "Order not found");
  14. }
  15. }

ApiParam

ApiParam请求属性,使用方式:
  1. public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true) User user)

与Controller中的方法并列使用。

属性配置:

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

Demo:

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

ApiResponse

ApiResponse:响应配置,使用方式:
  1. @ApiResponse(code = 400, message = "Invalid user supplied")

与Controller中的方法并列使用。

属性配置:

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

Demo:

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

ApiResponses

ApiResponses:响应集配置,使用方式:
  1. @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

与Controller中的方法并列使用。

属性配置:

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

Demo:

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

ResponseHeader

ResponseHeader:响应头设置,使用方法
  1. @ResponseHeader(name="head1",description="response head conf")

与Controller中的方法并列使用。

属性配置:

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

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注