Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 不兼容的类型列表和页面_Spring Boot_Jhipster - Fatal编程技术网

Spring boot 不兼容的类型列表和页面

Spring boot 不兼容的类型列表和页面,spring-boot,jhipster,Spring Boot,Jhipster,我正在使用JHipster开发一个Web应用程序来管理付款,并希望过滤用户可以看到的付款,以便他只能看到自己的付款。为此,我在Youtube上关注Matt Raible的报道。他使用由JHipster生成的findByUserIsCurrentUser List<Blog> blogs = blogRepository.findByUserIsCurrentUser(); 当我在我的项目中进行相同的更改时,我发现我必须返回的类型是一个页面,我得到一个不兼容的类型错误,下面是我的方法

我正在使用JHipster开发一个Web应用程序来管理付款,并希望过滤用户可以看到的付款,以便他只能看到自己的付款。为此,我在Youtube上关注Matt Raible的报道。他使用由JHipster生成的findByUserIsCurrentUser

List<Blog> blogs = blogRepository.findByUserIsCurrentUser();
当我在我的项目中进行相同的更改时,我发现我必须返回的类型是一个页面,我得到一个不兼容的类型错误,下面是我的方法:

public Page<Payment> findAll(Pageable pageable) {
    log.debug("Request to get all Payments");
    Page<Payment> result = paymentRepository.findByUserIsCurrentUser();
    return result;
}
如果我更改findByUserIsCurrentUser的findAllpageable,它在PaymentRepository中声明如下

public interface PaymentRepository extends JpaRepository<Payment,Long> {
    @Query("select payment from Payment payment where payment.user.login = ?#{principal.username}")
    List<Payment> findByUserIsCurrentUser();
}
我得到以下错误:


如何解决此问题?

您可以通过两种方式解决此问题。这是假设您正在使用一项服务,如果不是,它仍然非常相似:

方法1:如果您希望您的服务返回当前用户的所有付款,那么您需要修改您的服务,可能还有您的资源,以处理列表而不是页面:

public List<Payment> findAll() {
    log.debug("Request to get all Payments");
    List<Payment> result = paymentRepository.findByUserIsCurrentUser();
    return result;
}
然后,您需要将可分页对象从服务传入:

public Page<Payment> findAll(Pageable pageable) {
    log.debug("Request to get all Payments");
    Page<Payment> result = paymentRepository.findByUserIsCurrentUser(pageable);
    return result;
}

这将根据可分页对象包含的内容返回结果的子集。这包括大小、页码、偏移量和排序。例如,大小为20且页码为0将返回前20个结果。将页码更改为1将返回接下来的20个结果。这些参数通常是从前端传入API的-您会注意到PaymentResource中的getAllPayments有一个可分页的参数。

工作得非常好,非常感谢您的详细帮助!我使用了分页的解决方案。
public interface PaymentRepository extends JpaRepository<Payment,Long> {
    @Query("select payment from Payment payment where payment.user.login = ?#{principal.username}")
    Page<Payment> findByUserIsCurrentUser(Pageable pageable);
}
public Page<Payment> findAll(Pageable pageable) {
    log.debug("Request to get all Payments");
    Page<Payment> result = paymentRepository.findByUserIsCurrentUser(pageable);
    return result;
}