I've looked at both this and this question. But I have still not been able to setup paging for a repository method. Not sure if I'm affected by a bug or simply not writing this correctly. Basically I'm asking if someone could provide an example of how to implement paging on a repository method which gets exported through the @RepositoryRestResource annotation?
My attempt at achieving pagination
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByUserGroup(@Param("userGroup") String userGroup,
@Param("page") Pageable pageable);
}
The error message generated by the code
Offending method public abstract org.springframework.data.domain.Page com.project.repository.UserRepository.findByUserGroup(java.lang.String,java.awt.print.Pageable)
I've also tried removing the method param for pageable which then resulted in this error:
Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
Dependencies I'm using in this project.
Any help would be greatly appreciated.
Update: The final solution
Adding this as reference for anyone else wondering how to do this. The main difference was that I had to make sure to import the right Pageable
object as noted in the chosen answer.
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByUserGroup(@Param("userGroup") String userGroup, Pageable pageable);
}
Once we have our repository extending from PagingAndSortingRepository, we just need to: Create or obtain a PageRequest object, which is an implementation of the Pageable interface. Pass the PageRequest object as an argument to the repository method we intend to use.
PagingAndSortingRepository is an extension of CrudRepository to provide additional methods to retrieve entities using the pagination and sorting abstraction.
The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint. If we decided to omit it, Spring would automatically create an endpoint at “/websiteUsers” instead of “/users“. That's it! We now have a fully-functional REST API.
You are using the Pageable class from the wrong package: java.awt.print.Pageable
. You should be using org.springframework.data.domain.Pageable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With