首页 文章

Spring boot:如何在@RepositoryRestResource上配置分页?

提问于
浏览
6

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

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

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

任何帮助将不胜感激 .

Update: The final solution

添加此内容作为其他任何想知道如何执行此操作的参考 . 主要区别在于我必须确保导入正确的 Pageable 对象,如所选答案中所述 .

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends JpaRepository<User, Long> {

    Page<User> findByUserGroup(@Param("userGroup") String userGroup, Pageable pageable);

}

2 回答

  • 0

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

  • 13

    似乎很晚,但他们更容易解决分页问题 . 请参阅下面的代码段 .

    public interface NotebookRepository extends PagingAndSortingRepository<Notebook, Long> {
    

    有关完整示例,请通过此blog

相关问题