6

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

}
thunki
  • 69
  • 1
  • 4

2 Answers2

14

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

derkoe
  • 5,649
  • 2
  • 23
  • 31
0

Seems little late but their is easier solution for pagination. See below code snippet.

public interface NotebookRepository extends PagingAndSortingRepository<Notebook, Long> {

For complete example please go through this blog

shaILU
  • 2,050
  • 3
  • 21
  • 40