Conversion of List to Page in Spring

JavaSpringListPaginationSpring Data

Java Problem Overview


I am trying to convert list to page in spring. I have converted it using

> new PageImpl(users, pageable, users.size());

But now I having problem with sorting and pagination itself. When I try passing size and page, the pagination doesn't work.

Here's the code I am using.

My Controller

	public ResponseEntity<User> getUsersByProgramId(
		@RequestParam(name = "programId", required = true) Integer programId Pageable pageable) {

	List<User> users = userService.findAllByProgramId(programId);
	Page<User> pages = new PageImpl<User>(users, pageable, users.size());

	return new ResponseEntity<>(pages, HttpStatus.OK);
}

Here is my user Repo

public interface UserRepo extends JpaRepository<User, Integer>{

public List<User> findAllByProgramId(Integer programId);

Here is my service

	public List<User> findAllByProgramId(Integer programId);

Java Solutions


Solution 1 - Java

I had the same problem. I used subList:

final int start = (int)pageable.getOffset();
final int end = Math.min((start + pageable.getPageSize()), users.size());
final Page<User> page = new PageImpl<>(users.subList(start, end), pageable, users.size());

Solution 2 - Java

There is a Page implementation for that:

Page<Something> page = new PageImpl<>(yourList);

Solution 3 - Java

As indicated in the reference documentation, Spring Data repositories support pagination on query methods by simply declaring a parameter of type Pageable to make sure they're only reading the data necessary for the requested Page.

Page<User> page = findAllByProgramId(Integer programId, Pageable pageable);

That would return a Page object with the page size/settings defined in your Pageable object. No need to get a list and then try to create a page out of it.

Solution 4 - Java

You should do it like advised by the dubonzi's answer.

If you still want to use pagination for a given List use PagedListHolder:

List<String> list = // ...

// Creation
PagedListHolder page = new PagedListHolder(list);
page.setPageSize(10); // number of items per page
page.setPage(0);      // set to first page
 
// Retrieval
page.getPageCount(); // number of pages 
page.getPageList();  // a List which represents the current page
   

If you need sorting, use another PagedListHolder constructor with a MutableSortDefinition.

Solution 5 - Java

Try This:

public Page<Patient> searchPatientPage(SearchPatientDto patient, int page, int size){
		List<Patient> patientsList = new ArrayList<Patient>();
		Set<Patient> list=searchPatient(patient);
		patientsList.addAll(list);
		int start =  new PageRequest(page, size).getOffset();
		int end = (start + new PageRequest(page, size).getPageSize()) > patientsList.size() ? patientsList.size() : (start + new PageRequest(page, size).getPageSize());
		
		return new PageImpl<Patient>(patientsList.subList(start, end), new PageRequest(page, size), patientsList.size());
	}

Solution 6 - Java

This could be the solution. Sorting and pagination will work too this way:

Controller:

public ResponseEntity<User> getUsersByProgramId(
   @RequestParam(name = "programId", required = true) Integer programId Pageable pageable) {
       Page<User> usersPage = userService.findAllByProgramId(programId, pageable);
       Page<User> pages = new PageImpl<User>(usersPage.getContent(), pageable, usersPage.getTotalElements());

       return new ResponseEntity<>(pages, HttpStatus.OK);
}

Service:

Page<User> findAllByProgramId(Integer programId, Pageable pageable);

Repository:

public interface UserRepo extends JpaRepository<User, Integer>{
       public Page<User> findAllByProgramId(Integer programId, Pageable pageable);
}

This way, we can also return different page of entity too.

Solution 7 - Java

In the JHipster framework there is an interface for such things PageUtil:

static <T> Page<T> createPageFromList(List<T> list, Pageable pageable) {
    if (list == null) {
        throw new IllegalArgumentException("To create a Page, the list mustn't be null!");
    }

    int startOfPage = pageable.getPageNumber() * pageable.getPageSize();
    if (startOfPage > list.size()) {
        return new PageImpl<>(new ArrayList<>(), pageable, 0);
    }

    int endOfPage = Math.min(startOfPage + pageable.getPageSize(), list.size());
    return new PageImpl<>(list.subList(startOfPage, endOfPage), pageable, list.size());
}

Solution 8 - Java

Implemented based on @shilaimuslm comment. In this case an exception will not be thrown if the start > end in subList.

List<User> users = // ...

Pageable paging = PageRequest.of(pagePagination, sizePagination);
int start = Math.min((int)paging.getOffset(), users.size());
int end = Math.min((start + paging.getPageSize()), users.size());

Page<User> page = new PageImpl<>(users.subList(start, end), paging, users.size());

Solution 9 - Java

//1) For a boot application create a paging repository interface
public interface PersonRepository extends PagingAndSortingRepository<Person, 
String> {
// Common CURD method are automatically implemented
}
//2) create a service Interface
public interface PersonService {
    Page<Person> listAllByPage(Pageable pageable); // Use common CURD findAll() method
    Page<Object> listSpecByPage(Pageable pageable, String x);

}
//3) create a service Impl Class of service interface
@Service
public class PersonServiceImpl implements PersonService {

    final PersonRepository personRepository;
    
    @Autowired
    PersonServiceImpl(PersonRepository personRepository){
	this.personRepository = personRepository;
    }
   
    @Override
    public Page<Person> listAllByPage(Pageable pageable) {
	      return personRepository.findAll(pageable);
    }
    @Override
    public Page<Object> listSpecByPage(Pageable pageable, String path) {
	    List<Object> objectlist = new ArrayList<Object>();
        // Do your process to get output in a list by using node.js run on a *js file defined in 'path' varriable
	    Page<Object> pages1 = new PageImpl<Object>(objectlist, pageable, objectlist.size());
	    return pages1;
    }

}
//4) write your controller
public class PersonController {

    final PersonService personService;

    @Autowired
    PersonController( PersonService personService ){
	    this.personService = personService;
    }
    
    @GetMapping("/get") // Use of findALL() function
    Page<Person> listed( Pageable pageable){
        Page<Person> persons = personService.listAllByPage(pageable);
	    return persons;
    } 
    @GetMapping("/spec") // Use of defined function
    Page<Object> listSpec( Pageable pageable, String path){
	    Page<Object> obj = personService.listSpecByPage(pageable, path);
	    return obj;
   } 

}

Solution 10 - Java

Thanks guys below code is working in my case

	int start = pageble.getOffset();
	int end = (start + pageble.getPageSize()) > vehicleModelsList.size() ? vehicleModelsList.size() : (start + pageble.getPageSize());

Solution 11 - Java

Have you tried extending your repository to PagingAndSortingRepository?

public interface UserRepo extends PagingAndSortingRepository<Ticket, Integer> {
    Page<User> findAllByProgramId(Integer programId, Pageable pageable);
}

Service

Page<User> findAllByProgramId(Integer programId, Pageable pageable);

I assume you are using interface to the service:

Solution 12 - Java

Instead of returing complete array list take subblist as per your requirement. You will get 'offset' and size from 'pageable' object in request body.

new PageImpl<User>(users.subList(start, end), pageable, users.size());

Solution 13 - Java

This is the correct answer to pagging a list

public ResponseEntity<User> getUsersByProgramId(
            @RequestParam(name = "programId", required = true) Integer programId, Pageable pageable) {
        List<User> users = userService.findAllByProgramId(programId);

        final int toIndex = Math.min((pageable.getPageNumber() + 1) * pageable.getPageSize(),
					bidList.size());
		final int fromIndex = Math.max(toIndex - pageable.getPageSize(), 0);
        Page<User> pages = new PageImpl<User>(users.subList(fromIndex, toIndex), pageable, users.size());
    
        return new ResponseEntity<>(pages, HttpStatus.OK);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3127109View Question on Stackoverflow
Solution 1 - JavashilaimuslmView Answer on Stackoverflow
Solution 2 - JavaSachin GaurView Answer on Stackoverflow
Solution 3 - JavadubonziView Answer on Stackoverflow
Solution 4 - JavaWilli MentzelView Answer on Stackoverflow
Solution 5 - Javanaveen kumarView Answer on Stackoverflow
Solution 6 - JavaAshishView Answer on Stackoverflow
Solution 7 - JavaemaView Answer on Stackoverflow
Solution 8 - JavaPaul RozhkinView Answer on Stackoverflow
Solution 9 - JavaRBuser2769569View Answer on Stackoverflow
Solution 10 - JavaramView Answer on Stackoverflow
Solution 11 - JavaBorgy ManotoyView Answer on Stackoverflow
Solution 12 - JavasreeragView Answer on Stackoverflow
Solution 13 - JavaMartin Alvarado DiazView Answer on Stackoverflow