Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot: How to configure pagination on a @RepositoryRestResource?

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.

  1. Oracle java 8.
  2. "org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE",
  3. "org.springframework.boot:spring-boot-starter-web",
  4. "org.springframework.boot:spring-boot-starter-actuator",
  5. 'org.springframework.boot:spring-boot-starter-mail',
  6. "org.springframework.boot:spring-boot-starter-thymeleaf",
  7. "org.springframework.boot:spring-boot-starter-security",
  8. "org.springframework.security.oauth:spring-security-oauth2:2.0.0.RC2",
  9. "org.springframework.boot:spring-boot-starter-data-jpa",
  10. "org.springframework.boot:spring-boot-starter-data-rest",

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);

}
like image 972
thunki Avatar asked Apr 29 '15 09:04

thunki


People also ask

How will you implement pagination using spring?

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.

Which repository is used for pagination?

PagingAndSortingRepository is an extension of CrudRepository to provide additional methods to retrieve entities using the pagination and sorting abstraction.

What does the @RepositoryRestResource annotation do?

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.


1 Answers

You are using the Pageable class from the wrong package: java.awt.print.Pageable. You should be using org.springframework.data.domain.Pageable

like image 54
derkoe Avatar answered Sep 21 '22 15:09

derkoe