68

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.

Nima Ajdari
  • 2,754
  • 2
  • 14
  • 18
end-user
  • 2,845
  • 6
  • 30
  • 56
  • It is appears also at the [M3]http://docs.spring.io/spring-data/commons/docs/2.0.0.M3/api/org/springframework/data/domain/PageRequest.html#constructor.summary – KostasC Aug 03 '17 at 07:24

4 Answers4

200

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.

Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
Steffen
  • 3,999
  • 1
  • 23
  • 30
  • 3
    Spring Boot 2.0.0.RC1, For example: `Page currencyList = ccyRepository.findAll(PageRequest.of(evalPage, evalPageSize));` – Vy Do Feb 18 '18 at 02:40
  • Since Spring v2.0 use the static PageRequest::of method instead of constructing a new PageRequest instance. See more https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/PageRequest.html – Yurii Rabeshko Jul 22 '18 at 09: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. – Nima Ajdari Sep 19 '18 at 06:26
  • This will also work if you previously the constructor without passing in a Sort instance. – Roger Feb 13 '20 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 Oct 08 '21 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.

driveall
  • 301
  • 5
  • 12
11

You can use the following solution to solve your problem:

Page<User> users=userService.findByUserType(id,PageRequest.of(1, 3));
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Feroz Mujawar
  • 163
  • 2
  • 8
2

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)
Nullable
  • 761
  • 5
  • 17
Mounir bkr
  • 1,069
  • 6
  • 6