zoukankan      html  css  js  c++  java
  • swagger注释@API详细说明

    swagger注释@API详细说明

    swagger是当前最好用的Restful API文档生成的开源项目,通过swagger-spring项目实现了springMVC框架的无缝集成功能,方便生成restful风格的接口文档,

    同时,swagger-ui还可以测试spring restful风格的接口功能

    作用范围 API 使用位置
    对象属性 @ApiModelProperty 用在参数对象的字段上
    协议集描述 @Api 用在Conntroller类上
    协议描述 @ApiOperation 用在controller方法上
    Response集 @ApiResponses 用在controller方法上
    Response @ApiResponse 用在@ApiResponses里面
    非对象参数集 @ApilmplicitParams 用在controller方法上
    非对象参数描述 @ApiImplicitParam 用在@ApiImplicitParams的方法里边
    描述返回对象的意义 @ApiModel 用在返回对象类上

    1 @ApiModelProperty

    value url的路径值
    name 重写属性名字
    dataType 重写属性类型
    required 是否必填
    example 举例说明
    hidden 隐藏
    //  我这个用在实体类的get()方法上了
    /**
         * 获取城市编号
         * @return 城市编号
         */
        @ApiModelProperty(value="城市编号",example="058",required=true)
        public String getCode() {
            return code;
        }
    
        /**
         * 设置城市编号
         * @param code  城市编号
         */
        public void setCode(String code) {
            this.code = code;
        }
    
        /**
         * 获取城市名称
         * @return 城市名称
         */
        @ApiModelProperty(value="城市名称",example="guangZhou",required=true)
        public String getName() {
            return name;
        }
    

    2 @api

    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 将在文档中隐藏
    @Controller
    @RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
    @Api(value = "/pet", description = "Operations about pets")
    public class PetController {
    }
    

    3 @ApiOperation

    属性名称 备注
    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 扩展属性
    @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");
        }
      }
    

    4 @ApiParam标记

    属性名称 备注
    name 属性名称
    value 属性值
    defaultValue 默认属性值
    allowableValues 可以不配置
    required 是否属性必填
    access 不过多描述
    allowMultiple 默认为false
    hidden 隐藏该属性
    example 举例子
     public ResponseEntity<Order> getOrderById(
          @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)
          @PathVariable("orderId") String orderId)
    

    5 @ApiResponse

    属性名称 备注
    code http的状态码
    message 描述
    response 默认响应类 Void
    reference 参考ApiOperation中配置
    responseHeaders 参考 ResponseHeader 属性配置说明
    responseContainer 参考ApiOperation中配置
     @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 @ApiResponses

    属性名称 备注
    value 多个ApiResponse配置
     @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("");
      }
    

    7 @ResponseHeader

    属性名称 备注
    name 响应头名称
    description 头描述
    response 默认响应类 Void
    responseContainer 参考ApiOperation中配置
    @ApiModel(description = "群组")
    

    8 其他

    • @ApiImplicitParams:用在方法上包含一组参数说明;
    • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
      • paramType:参数放在哪个地方
      • name:参数代表的含义
      • value:参数名称
      • dataType: 参数类型,有String/int,无用
      • required : 是否必要
      • defaultValue:参数的默认值
    • @ApiResponses:用于表示一组响应;
    • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息;
      • code: 响应码(int型),可自定义
      • message:状态码对应的响应信息
    • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候;
    • @ApiModelProperty:描述一个model的属性。
  • 相关阅读:
    SDK Hello world(直接使用SDK封装)
    一个类有多个基类的内存布局
    写个测试程序看看磁盘映像文件中哪个扇区可以使用?
    在服务中以当前用户身份启动一个程序
    在类有成员变量的场景下, 按照虚表原理, 模拟虚函数实现
    pipe----管道
    Qt序列化格式分析(qint,QString)(非常简单好用)
    模拟QQ系统设置面板实现功能
    Qt持久性对象进行序列化(同时比较了MFC与Java的方法)
    使用srvany.exe将任何程序作为Windows服务运行
  • 原文地址:https://www.cnblogs.com/yoyo1216/p/13840303.html
Copyright © 2011-2022 走看看