70

I'm working the Spring Data Commons v2+ snapshot, and I see that the constructors for a PageRequest have been deprecated. This appears to have occurred between M1 & M2. Unfortunately, this is the only [real] implementation of the Pageable interface. I'm wondering where the effort is heading, and what a better alternative would be for current development.

1

4 Answers 4

204

It's just the constructors which have been deprecated. Instead of

new PageRequest(firstResult, maxResults, new Sort(...))

you can now use

PageRequest.of(firstResult, maxResults, Sort.by(...))

and that's it.

5
  • 3
    Spring Boot 2.0.0.RC1, For example: Page<Ccy> currencyList = ccyRepository.findAll(PageRequest.of(evalPage, evalPageSize));
    – Vy Do
    Commented Feb 18, 2018 at 2:40
  • Since Spring v2.0 use the static PageRequest::of method instead of constructing a new PageRequest instance. See more docs.spring.io/spring-data/commons/docs/current/api/org/… Commented Jul 22, 2018 at 9:33
  • I wonder why the Spring Data reference is still using the deprecated constructors in the examples, given that it has been more than a year they are deprecated. Commented Sep 19, 2018 at 6:26
  • This will also work if you previously the constructor without passing in a Sort instance.
    – Roger
    Commented Feb 13, 2020 at 19:38
  • Yep, and I wonder why the javadoc does not tell anything about the deprecation reason and does not mention the new factory methods.
    – Christian
    Commented Oct 8, 2021 at 15:33
13

We can use PageRequest.of(offset, limit) instead of new PageRequest(offset, limit). In this case we don't need to use deprecated constructor.

11

You can use the following solution to solve your problem:

Page<User> users=userService.findByUserType(id,PageRequest.of(1, 3));
1
  • 4
    Please provide an explaination of why this solves the problem.
    – georgeawg
    Commented Sep 1, 2018 at 18:57
3

Since Spring v2.0: PageRequest.of() is a static method , you don't need to construct a new PageRequest() instance.

use this static methode for Creating a new unsorted PageRequest :

PageRequest.of(int page, int size)

Not the answer you're looking for? Browse other questions tagged or ask your own question.