Conversion of List to Page in Spring

2019-02-11 11:53发布

问题:

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);

回答1:

I have meet the same question. I solved this using subList, that is:

int start = pageable.getOffset();
int end = (start + pageable.getPageSize()) > users.size() ? users.size() : (start + pageable.getPageSize());
Page<User> pages = new PageImpl<User>(users.subList(start, end), pageable, users.size());


回答2:

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.



回答3:

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.



回答4:

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:



回答5:

//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;
   } 

}


回答6:

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());
    }


回答7:

There is a Page implementation for that:

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


回答8:

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.



回答9:

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());