後端與React前端對接的分頁規範寫法

1.  獲取page。

2. 使用PageUtils來獲取一個HttpHeaders 。

3.最後在ResponseEntity裏面添加三個參數:l ists,totalHeader, HttpStatus.OK

package com.hand.hcf.app.workflow.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.hand.hcf.app.workflow.domain.WorkflowBackReport;
import com.hand.hcf.app.workflow.service.WorkflowBackReportService;
import com.hand.hcf.core.util.PageUtil;
import com.sun.deploy.net.HttpUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Pageable;

import java.util.List;
@RestController
@RequestMapping("/api")
@Api("審批退回數據查詢")
public class WorkflowBackReportController { 

@GetMapping("/query/all/back/report")
    public ResponseEntity<List<WorkflowBackReport>> queryAllBackReport(Pageable pageable) 
   {
        Page page = PageUtil.getPage(pageable);
        HttpHeaders totalHeader = PageUtil.getTotalHeader(page);
        List<WorkflowBackReport> lists=workflowBackReportService.queryAllBackReport(page);
        return new ResponseEntity<>(lists,totalHeader, HttpStatus.OK);
    }

}

如果總共查到了7條數據,那麼會在響應頭裏拿到總數:  X-Total-Count: 7

HTTP/1.1 200 OK

Date: Fri, 19 Jun 2020 08:24:25 GMT

Content-Type: application/json;charset=UTF-8

Content-Length: 645

Connection: keep-alive

Server: nginx

Cache-Control: no-cache, no-store, max-age=0, must-revalidate

Content-Encoding: gzip

Expires: 0

Pragma: no-cache

Vary: Accept-Encoding

X-Content-Type-Options: nosniff

X-Frame-Options: DENY

X-Total-Count: 7

X-Xss-Protection: 1;

mode=block

 

前端再根據 X-Total-Count 來獲取到總數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章