【问题标题】:Spring boot: How to configure pagination on a @RepositoryRestResource?Spring boot:如何在@RepositoryRestResource 上配置分页?
【发布时间】:2015-07-08 10:59:51
【问题描述】:

我查看了thisthis 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!

我在这个项目中使用的依赖项。

  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",

任何帮助将不胜感激。

更新:最终解决方案

将此添加为其他想知道如何执行此操作的人的参考。主要区别在于我必须确保导入正确的 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


    【解决方案1】:

    您使用了错误包中的 Pageable 类:java.awt.print.Pageable。你应该使用org.springframework.data.domain.Pageable

    【讨论】:

    • 好发现!这正是问题所在。谢谢! :)
    【解决方案2】:

    似乎有点晚了,但他们更容易解决分页。见下面代码sn-p。

    public interface NotebookRepository extends PagingAndSortingRepository<Notebook, Long> {
    

    完整示例请通过blog

    【讨论】:

    • 我可以看到人们访问我的博客,如果此解决方案有效,请投票。
    猜你喜欢
    • 2018-01-29
    • 2014-10-07
    • 2017-08-31
    • 2014-08-25
    • 2017-02-14
    • 1970-01-01
    • 2015-10-29
    • 2014-07-26
    • 1970-01-01
    相关资源
    最近更新 更多