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

标签 rest api pagination swagger

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

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

最佳答案

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

这是一个例子——

/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

关于rest - 在 REST API 的 Swagger 定义中实现分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53650266/

相关文章:

javascript - 通过 Javascript 访问 Github Pages Secret Api Keys 或包含在 html 标记中

javascript - 为什么 jQuery 没有应用于 Laravel 从第 2 页开始的分页结果

javascript - 使用 Flickr API 获取指定照片集的标签列表

java - java中rest API的身份验证失败错误

java - 在没有 JSP 的情况下执行 Spring MVC 的最佳方法?

rest - REST API 如何有效地处理分页?

javascript - 模拟服务 worker ,悲伤的路径测试失败

php - 服务器和客户端分页到底是什么?

jquery - 使用 Previous、Next 控件在柱形图中分页?

rest - Sinatra 和 AngularJS 之间的 CORS 问题