简体   繁体   English

在 Spring Boot 中调用分页的 REST 请求

[英]Call paginated REST request in spring boot

I'm building a spring boot application that needs to make REST call which has pagination implemented, like:我正在构建一个 Spring Boot 应用程序,它需要进行 REST 调用,该调用实现了分页,例如:

http://host/api/getlist?p=1&ps=100 http://host/api/getlist?p=1&ps=100

where p is page number & ps is page size.其中 p 是页码,ps 是页面大小。

The result of this api apart from output list also displays paging info like:除了输出列表之外,此 api 的结果还显示分页信息,例如:

"paging": {
        "pageIndex": 1,
        "pageSize": 100,
        "total": 372
    }

Currently, in my first hit I'm retrieving the output list and also reading this paging info and then calculating how many further hits are required.目前,在我的第一次点击中,我正在检索输出列表并阅读此分页信息,然后计算需要多少次进一步点击。 In above case, 372 / 100 = 3 , then 3 - 1 , because one hit is already done.在上述情况下, 372 / 100 = 3 ,然后是3 - 1 ,因为已经完成了一次命中。

But this is very naive approach.但这是非常幼稚的做法。 Is there something that spring / java provides, that I can use? spring/java 是否提供了一些我可以使用的东西?

Please suggest.请建议。

Thanks谢谢

@reiley Implement Paging using Pageable interface. @reiley 使用 Pageable 接口实现分页。

It gives other detail also so it become helpful to perform other action.它还提供了其他细节,因此执行其他操作会很有帮助。

Front end page size start from 1 and backend it start from 0.前端页面大小从 1 开始,后端从 0 开始。

Controller :控制器 :

@GetMapping("/")
  private ResponseEntity<Page<User>> getUsers(@RequestParam(value = "page_no", defaultValue = "1") Integer pageNo,
                                              @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                              @RequestParam(value = "order_by", defaultValue = "userId") String sortBy,
                                              @RequestParam(value = "order_type", defaultValue = "asc") String orderBy) {
    Page<UserDTO> users = userService.getUsers(pageNo, limit, sortBy, orderBy);
    return customResponse(true, Constants.DATA_FETCH, ENTITY_NAME.concat("s"), users);
  }

ServiceImpl服务实现

@Override
  public Page<UserDTO> getUsers(Integer pageNo, Integer limit, String sortBy, String orderBy) {

    Sort sortingInOrder = Util.sortingOrder(orderBy, sortBy);
    Pageable pageable = PageRequest.of(pageNo - 1, limit, sortingInOrder);
    Page<UserDTO> result = userRepository.findAll(pageable).map(user -> userMapper.toDTO(user));
    if (result.getContent().size() > 0) {
      return result;
    }
    return Page.empty();
  }

Util实用程序

public static Sort sortingOrder(String orderBy, String sortBy) {
    Sort sortingInOrder;
    switch (orderBy) {
      case "asc":
        sortingInOrder = Sort.by(sortBy).ascending();
        break;
      case  "desc" :
        sortingInOrder = Sort.by(sortBy).descending();
        break;
      default:
        throw new CustomException("Invalid Input in order by!!!");
    }
    return sortingInOrder;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM