【发布时间】:2015-07-08 10:59:51
【问题描述】:
我查看了this 和this question。但是我仍然无法为存储库方法设置分页。不确定我是否受到错误的影响,或者根本没有正确编写。基本上我在问是否有人可以提供一个示例,说明如何在通过 @RepositoryRestResource 注释导出的存储库方法上实现分页?
我的分页尝试
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByUserGroup(@Param("userGroup") String userGroup,
@Param("page") Pageable pageable);
}
代码生成的错误信息
Offending method public abstract org.springframework.data.domain.Page com.project.repository.UserRepository.findByUserGroup(java.lang.String,java.awt.print.Pageable)
我还尝试删除 pageable 的方法参数,然后导致此错误:
Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
我在这个项目中使用的依赖项。
- Oracle java 8.
- "org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE",
- "org.springframework.boot:spring-boot-starter-web",
- "org.springframework.boot:spring-boot-starter-actuator",
- 'org.springframework.boot:spring-boot-starter-mail',
- "org.springframework.boot:spring-boot-starter-thymeleaf",
- "org.springframework.boot:spring-boot-starter-security",
- "org.springframework.security.oauth:spring-security-oauth2:2.0.0.RC2",
- "org.springframework.boot:spring-boot-starter-data-jpa",
- "org.springframework.boot:spring-boot-starter-data-rest",
任何帮助将不胜感激。
更新:最终解决方案
将此添加为其他想知道如何执行此操作的人的参考。主要区别在于我必须确保导入正确的 Pageable 对象,如所选答案中所述。
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByUserGroup(@Param("userGroup") String userGroup, Pageable pageable);
}
【问题讨论】:
标签: pagination spring-boot spring-data-jpa spring-data-rest