使用RAML规范的RESTAPI:如何指定响应中的总页面

使用RAML规范的RESTAPI:如何指定响应中的总页面,rest,api,raml,Rest,Api,Raml,我不确定RESTAPI在分页方面应该如何设计 下面是我的例子: #%RAML 1.0 title: Test Api Documentation description: Test Api Documentation baseUri: https://localhost/ version: v0.1 protocols: [ HTTPS ] mediaType: [ application/json ] documentation: - title: Test API for REST Cl

我不确定RESTAPI在分页方面应该如何设计

下面是我的例子:

#%RAML 1.0
title: Test Api Documentation
description: Test Api Documentation
baseUri: https://localhost/
version: v0.1
protocols: [ HTTPS ]
mediaType: [ application/json ]
documentation:
  - title: Test API for REST Client
    content:
      "Test API for REST Client."
types:
  Thing:
    type: object
    properties:
      name: string
      age: number
      color: string
  Things:
    type: object
    properties:
      things?: Thing[]
      totalPages?:
        type: integer
        format: int64
/things:
  get:
    headers:
      Cookie:
        type: string
    responses:
      '200':
        description: OK
        body:
          application/json:
            type: Things
      '401':
        description: Unauthorized
      '404':
        description: Not Found
    queryParameters:
      page:
        description: page
        required: false
        type: integer
        format: int32
      pageSize:
        description: pageSize
        required: false
        type: integer
        format: int32
有这样一种包装类型“Things”,可以为响应添加totalpages属性

这样做对吗

我还了解到可以使用自定义http头(x-total-pages或类似的东西)

实际上我不喜欢API中的所有包装类型。。。 有人知道这是什么标准吗

非常感谢您的回答。
Sergio

您可以将所有分页的trait封装到
trait
中,并使用
is
在所有可分页的资源中使用该特性。由于可分页资源通常是GET/collectionresource,您还可以为200个分页的
响应的响应标题X-TOTAL-PAGES添加一个特征

例如:

#%RAML 1.0
baseUri: https://mocksvc.qax.mulesoft.com/mocks/8ab3d909-11e0-4f1d-aaef-bef029b83fbf
title: paginated api
mediaType: application/json
protocols: [ HTTP ]
traits:
  paginated:
      queryParameters:
        page:
          description: page
          required: false
          type: integer
          format: int32
        pageSize:
          description: pageSize
          required: false
          type: integer
          format: int32    
      responses:
        200:
          headers:
            x-total-pages:  
types:
  Thing:
    type: object
    properties:
      name: string
      age: number
      color: string
  Things:
    type: object
    properties:
      things?: Thing[]

/things:
  get:
    is: [ paginated ]
    responses:
      '200':
        description: OK
        body:
          application/json:
            type: Things
      '401':
        description: Unauthorized
      '404':
        description: Not Found 

您可以将所有分页的trait封装到
trait
中,并使用
is
在所有可分页资源中使用该特性。由于可分页资源通常是GET/collectionresource,您还可以为200个分页的
响应的响应标题X-TOTAL-PAGES添加一个特征

例如:

#%RAML 1.0
baseUri: https://mocksvc.qax.mulesoft.com/mocks/8ab3d909-11e0-4f1d-aaef-bef029b83fbf
title: paginated api
mediaType: application/json
protocols: [ HTTP ]
traits:
  paginated:
      queryParameters:
        page:
          description: page
          required: false
          type: integer
          format: int32
        pageSize:
          description: pageSize
          required: false
          type: integer
          format: int32    
      responses:
        200:
          headers:
            x-total-pages:  
types:
  Thing:
    type: object
    properties:
      name: string
      age: number
      color: string
  Things:
    type: object
    properties:
      things?: Thing[]

/things:
  get:
    is: [ paginated ]
    responses:
      '200':
        description: OK
        body:
          application/json:
            type: Things
      '401':
        description: Unauthorized
      '404':
        description: Not Found 

这正是我想要的。Thx很多。这正是我想要的。非常感谢。