Swagger 常用注解使用讲解

讲解内容

  1. swagger常用到的注解的解释
  2. SpringMVC中控制层类中使用这些注解

swagger常用到的注解的解释

在pom.xml文件中添加包依赖:swagger-annotations-1.5.10.jar

所有注解:

输入图片说明

常用到的注解有:

  • Api
  • ApiModel
  • ApiModelProperty
  • ApiOperation
  • ApiParam
  • ApiResponse
  • ApiResponses
  • ResponseHeader

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 {
    
}

定义后swagger效果图: 

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的值会被覆盖 
notes对api资源的描述 
response返回的对象 
responseContainer这些对象是有效的 "List", "Set" or "Map".,其他无效 
responseReference可以不配置 
httpMethod可以接受 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH" 
position如果配置多个Api 想改变显示的顺序位置 
produces同 Api中的定义 
consumes同 Api中的定义 
protocols同 Api中的定义 
authorizations同 Api中的定义 
hidden同 Api中的定义 
codehttp的状态码 默认 200 
extensions扩展属性 

在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");
    }
  }

swagger效果图: 输入图片说明

ApiParam请求属性

使用方式:

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举例子 
examples  

在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)

ApiResponse响应配置

使用方式:

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

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

属性名称备注默认值
codehttp的状态码 
message描述 
response默认响应类 Void 
reference参考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("");
  }

效果图展示: 输入图片说明

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("");
  }

ResponseHeader相应配置

使用方式:

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

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

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

在SpringMvc中的配置如下:

@ApiModel(description = "群组")

ApiModel相应配置

使用方式:

description="描述实体的作用"  

属性配置:

属性名称备注默认值
value默认为类的名称 
description对该类的描述 
parent可以不配置 
discriminator可以不配置 
subTypes可以不配置 
reference引用配置可以不考虑 

在SpringMvc中的配置如下:

@ApiModel(description = "群组")
public class UamGroup {}

ApiModelProperty相应配置

使用方式:

@ApiModelProperty(required = true, value = "群组的Id")

属性配置:

属性名称备注默认值
value属性描述 
name如果配置覆盖属性名称 
allowableValues参考ApiParam配置项项 
access可以不配置 
notes没有使用 
dataType数据类型 
required参考ApiParam配置项项 
position参考ApiOperation配置项 
hidden参考ApiOperation配置项 
example参考ApiParam配置项项 
readOnly  
reference参考ApiParam配置项项 

在SpringMvc中的配置如下:

@ApiModelProperty(required = true, value = "群组的Id")
    private String groupId;
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值