Reputation: 41
I want to get the Results Paginated, The below code works fine if the request is made with page parameter set to 0 but it doesn't work for page>0 like page=1 or page=2 or page=3 etc.
Here is my RequestMapping
MyResponse getSample(@ModelAttribute MyRequest MyRequest) {
Pageable pageRequest = new PageRequest(MyRequest.page, MyRequest.size)
MyModule.findSamples(MyRequest, pageRequest)
}
class MyRequest {
MyQueryType queryType
String searchTerm
@Min(value = 0L, message = 'Offset must be greater than or equal to 0')
int offset = 0
@Min(value = 0L, message = 'Offset must be greater than or equal to 0')
int page = 0
@Min(value = 1L, message = 'Limit must be greater than or equal to 1')
int limit = 100
@Min(value = 1L, message = 'Limit must be greater than or equal to 1')
int size = 5
}
MyModule:Code inside my Module
MyResponse findSamples(MyRequest MyRequest, Pageable pageRequest) {
log.info("Page Information Set "+pageRequest.pageNumber+pageRequest.pageSize)
Page<SamplesPO> pages = null
pages = MyRepository.findAllById(MyRequest.Id, pageRequest)
}
Repository Code:
public interface SampleRepository extends JpaRepository<Sample, Long> {
@Query('''
select e.Samples
from ParentSampleTable e
where e.Id = :Id
''')
Page<Sample> findAllById(@Param('Id') String Id, Pageable pageRequest)
}
Upvotes: 2
Views: 11869
Reputation: 91
Read PageRequest java docs before Using below code. PageRequest request works on page number(starts from index 0 to 1,2,3 and so on) and the size( limit you want)
Sort sort = new Sort(Sort.Direction.DESC, "mfgDate");
Pageable pageable = new PageRequest(pageNumber, pageSize, sort);
@Repository
public interface BikeRepository extends MongoRepository<Bike, String> {
Page<Bike> findByCompanyId(String companyId, Pageable pageable);
}
Upvotes: 3
Reputation: 372
You could try extracting the id
with @PathParam
Method getSample
annotated with @GetMapping
with params (@PathParam("id") String id, Pageable pageable)
and it could return sampleRepository.findAllById(id, pageable)
Url might look like: /samples/id?page=pageNr&size=nrOfElemOnPage
Your repository should return
Page<Sample> findAllById(String id, Pageable pageable)
Also please consider refactoring(indention) the code before posting it to stackoverflow
Also this might be a duplicate: Using findAll PagingAndSortingRepository with filter
Upvotes: 0