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.
-
It is appears also at the [M3]docs.spring.io/spring-data/commons/docs/2.0.0.M3/api/org/…– KostasCCommented Aug 3, 2017 at 7:24
Add a comment
|
4 Answers
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.
-
3Spring Boot 2.0.0.RC1, For example:
Page<Ccy> currencyList = ccyRepository.findAll(PageRequest.of(evalPage, evalPageSize));
– Vy DoCommented 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.– RogerCommented 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. Commented Oct 8, 2021 at 15:33
We can use PageRequest.of(offset, limit)
instead of new PageRequest(offset, limit)
.
In this case we don't need to use deprecated constructor.
You can use the following solution to solve your problem:
Page<User> users=userService.findByUserType(id,PageRequest.of(1, 3));
-
4Please provide an explaination of why this solves the problem. Commented Sep 1, 2018 at 18:57
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)