[英]Spring Boot JPA Pagination Exception
我正在為我的服務實現分頁,其中限制和偏移量是請求正文的一部分。 在服務級別的PageRequest中設置這些值后,出現以下錯誤和實現
Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
at org.springframework.util.Assert.isTrue(Assert.java:92) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.data.repository.query.Parameters.assertEitherAllParamAnnotatedOrNone(Parameters.java:297) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:91) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaParameters.<init>(JpaParameters.java:43) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:325) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.createParameters(JpaQueryMethod.java:53) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:77) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:89) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 55 common frames omitted
學生要求:
{
"name": "string",
"paging": {
"limit": 0,
"startOffset": 0
}
}
學生服務:
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> getAllStudents(StudentRequest studentRequest) {
List<Student> studentList = studentRepository.getAll(studentRequest,new PageRequest(studentRequest.getPaging().getStartOffset(), studentRequest.getPaging().getLimit()));
return studentList;
}
}
學生資料庫:
@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {
@Query(value = "select o from Student o where (o.name = :#{#studentRequest.name} OR :#{#studentRequest.name} IS NULL ) ")
public List<Student> getAll(@Param("studentRequest") StudentRequest studentRequest, PageRequest pageRequest);
}
檢查錯誤:
use @Param on all parameters except Pageable and Sort typed once, or none at all!
這意味着您不能在Pageable
(或PageRequest
)和Sort
參數中使用@Param
,它們將由spring自動處理。
只需刪除@Param("pageRequest")
。
提示:使用Pageable
接口而不是PageRequest
類。
看起來失敗的斷言只是希望您具有AllParamAnnotatedOrNone
。 所以你會做:
public List<Student> getAll(@Param("studentRequest") StudentRequest studentRequest,
@Param("pageRequest") PageRequest pageRequest);
即使您沒有使用它; 您只需要一個模式(即名為Spring Data參數)或其他模式(即推斷出的Spring Data參數名稱)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.