How to get List from Page in Spring Data REST
The simplest way to fetch all your results at once is to use Pageable.unpaged(), like below:
Page<Employee> allEmployees = employeeRepository.findAll(Pageable.unpaged());
allEmployees.getContent();
But if you are concerned about retrieving too much data at once and instead prefer to do the retrieval in small chunks, you can use a service or repository method similar to below:
private List<Employee> findAll() {
List<Employee> allEmployees = new ArrayList<>();
// First page of employees -- 5000 results per page
PageRequest pageRequest = PageRequest.of(0, 5000);
Page<Employee> employeePage = employeeRepository.findAll(pageRequest);
allEmployees.addAll(employeePage.getContent());
// All the remaining employees
while (employeePage.hasNext()) {
Page<Employee> nextPageOfEmployees = employeeRepository.findAll(employeePage.nextPageable());
allEmployees.addAll(nextPageOfEmployees.getContent());
// update the page reference to the current page
employeePage = nextPageOfEmployees;
}
return allEmployees;
}
Check this page for advantages and disadvantages on fetching the data in chunks.
I know I am late, but, this is the first option it pops in google and people may need more answers when all the information of all pages needs to be converted into a list...
One aproximation can be:
xRepository.findAll(new PageRequest(0, Integer.MAX_VALUE)).getContent();
The problem here is that spring sets the maximum size to 1000 so you are going to get a list of maximum 1000 elements.
An other way of doing it can be multiple find with diferent page indexes and then adding the results into a list with getContent():
Page<T> pageData = xRepository.findAll(new PageRequest(0, 20));
List<T> finalList = pageData.getContent();
while(!pageData.isLast()){
pageData = xRepository.findAll(pageData.nextPageable());
List<T> listData = pageData.getContent();
//append listData into finalList
}
If you use pageable in a jpa repository method, spring will always return a Page not a List. I suggest you have a service method that calls the repository method and extracts the contents of the Page result into a list.
So if your repository method is thus:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RecordRepository extends JpaRepository<Record, Integer>{
Page<Record> findAll(Pageable pageable);
}
then you can have a service class which has a method that calls the repository method
@Service
public class RecordService{
@Autowired
RecordRepository recordRepository;
public List<Record> findAll(PageRequest pageRequest){
Page<Record> recordsPage = recordRepository.findAll(pageRequest);
return recordsPage.getContent();
}
}
so in your calling class instead of calling the repository directly you can just use the service. thus:
public class MyRecordImpl{
@Autowired
RecordService recordService;
public void doSomething(){
int page = 0; int pageSize = 5;
List<Record> recordList = recordService.findAll(new PageRequest(page, pageSize, new Sort(Sort.Direction.DESC, "recordId")));
//do other implementations here
}
}