Search code examples
springspring-mvcspring-bootspring-restcontroller

Set limits to Pageable on Spring Boot


I've got a

@ResponseBody
@Produces("application/json")
@RequestMapping(value = "/users", method = RequestMethod.GET)
public Page<User> getRecentUsers(Pageable pageable,
                                    HttpServletResponse response){
    return userRepository.findAll(pageable);
}

Can I set limits to the pageSize of the pageable automatically, without having to accept page and pageSize indicidually?


Solution

  • you can modify the pageable object before passing it to findAll.

    Being an interface anyway you have to build a new object with it. Something like:

    Pageable wrapped = new PageRequest(pageable.getPage(), Math.min(pageable.getSize(), **maxsize**), pageable.getSort());
    return userRepository.findAll(wrapped);