我正在为我的服务实现分页,其中限制和偏移量是请求正文的一部分。在服务级别的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!
这意味着您不能在@Param
(或Pageable
)和PageRequest
参数中使用Sort
,它们会由spring自动处理。
只需删除@Param("pageRequest")
,它将起作用。
提示:使用Pageable
界面而不是PageRequest
类。