-3

I'm building a spring boot application that needs to make REST call which has pagination implemented, like:

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

where p is page number & ps is page size.

The result of this api apart from output list also displays paging info like:

"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.

But this is very naive approach. Is there something that spring / java provides, that I can use?

Please suggest.

Thanks

reiley
  • 3,759
  • 12
  • 58
  • 114
  • 1
    It has nothing naive. It just use simple (but incorrect) arithmetics. If there are 372 elements, and the page size is 100, you need to make a total of 4 requests, not 3: one for elements 1 to 100, one for elements 101 to 200, one for elements 201 to 300, and one for elements 301 to 372. – JB Nizet Jan 24 '20 at 08:29
  • @JBNizet, yes I meant that only, just am more focused on if there is other cleaner solution. – reiley Jan 24 '20 at 08:32
  • 1
    No, there isn't. – JB Nizet Jan 24 '20 at 08:34
  • 1
    Graphql alternative :https://graphql.org/learn/pagination/ it will gives your more felxibility what do you want to load – Rebai Ahmed Jan 24 '20 at 10:01
  • There are several alternative approaches here https://stackoverflow.com/questions/34647303/spring-resttemplate-with-paginated-api – Shafiul Jan 24 '20 at 10:25

1 Answers1

0

@reiley Implement Paging using Pageable interface.

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.

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;
  }
Dhwanil Patel
  • 2,273
  • 1
  • 18
  • 28