在 REST API 的 Swagger 定义中实现分页

Arp*_*sal 7 api rest pagination swagger

我想在我的 REST API 响应中实现分页。对于 DTO 和控制器,我使用 Swagger 生成。如何指定我需要为一个特定对象进行分页。

对于分页,我是否需要一次又一次地调用 API(对于每个页面请求)?因为 API 还会执行其他功能(包括数据库访问和存储),所以它不会对系统造成太大影响吗?

Dat*_*ray 13

您可以定义参数来定义起始索引和页面长度。响应应包括总记录和编号。当前显示的记录数。

这是一个例子——

/orders:
    get:
      tags:
        - orders
      summary: Get details about the customer orders
      description: ''
      operationId: getOrders
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - name: customerid
          in: query
          description: Id of the customer 
          required: true
          type: string
          collectionFormat: multi
        - name: ordered
          in: query
          description: orderid for the order  
          required: false
          type: string
          collectionFormat: multi
        - $ref: "#/parameters/Startindex"
        - $ref: "#/parameters/Pagelength"
      responses:
        200:
          description: An array of customer orders
          schema:
             type: object
             allOf:
               - $ref: '#/definitions/PaginationResponse'
               - properties:
                   devices:
                     type: array
                     items:
                       $ref: '#/definitions/Order’
        '400':
          description: Invalid ID supplied
        '404':
          description: Customer not found
        '405':
          description: Validation exception


definitions:
……
…..
 PaginationResponse:
    type: object
    properties:
      totalrecords:
        type: number
      displayrecords:
         type: number
    xml:
       name: PaginationResponsedata


parameters:
    Pagelength:
      name: pagelength
      in: query
      description: Number of records to return
      type: number
    Startindex:
      name: startindex
      in: query
      description: Start index for paging
      type: number
Run Code Online (Sandbox Code Playgroud)