PageRequest constructors have been deprecated

JavaSpringSpring DataSpring Data-Commons

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

You can use the following solution to solve your problem:

Page<User> users=userService.findByUserType(id,PageRequest.of(1, 3));

Solution 4 - Java

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionend-userView Question on Stackoverflow
Solution 1 - JavaSteffenView Answer on Stackoverflow
Solution 2 - JavadriveallView Answer on Stackoverflow
Solution 3 - JavaFeroz MujawarView Answer on Stackoverflow
Solution 4 - JavaMounir bkrView Answer on Stackoverflow