I want to make paginable service with filter but its fail to resultset
Here is my controller
#GetMapping(path = "/PageFilter")
public DataResponsePagination<HistoryBankWrapper, HistoryBank> pageFilter(
#RequestParam("filter") String keyfilter ,
#RequestParam("sortField") String field ,
#RequestParam("sortOrder") String order ,
#RequestParam("page") int page,
#RequestParam("size") int size) {
return new DataResponsePagination<HistoryBankWrapper, HistoryBank>(historyBankService.findByFilter(keyfilter,field,order, page, size));
}
Here is my service
public PaginationList<HistoryBankWrapper, HistoryBank> findByFilter(String keyfilter,String sortField,String sortOrder, int page, int size) {
Pageable paging = PageRequest.of(page, size);
Page<HistoryBank> historyPage = historyBankRepository.findAllFilter(keyfilter, sortField, sortOrder, paging);
List<HistoryBank> historyList = historyPage.getContent();
List<HistoryBankWrapper> historyWrapperList = toWrapperList(historyList);
return new PaginationList<HistoryBankWrapper, HistoryBank>(historyWrapperList, historyPage);
}
here is my Repository
#Query(value = "SELECT * FROM HISTORY_BANK WHERE :sortField LIKE '%' || :keyFilter || '%' ORDER BY :sortField :sortOrder",
countQuery = "SELECT count(*) FROM HISTORY_BANK",
nativeQuery = true)
Page<HistoryBank> findAllFilter(#Param("keyFilter") String keyfilter, #Param("sortOrder") String sortOrder,#Param("sortField") String sortField, #Param("paging") Pageable paging);
Ditch your own method and use the framework. Use specifications to create a dynamic query.
Your repository should extend the JpaSpecificationExecutor and then you should invoke the findAll(Specification, Pageable) from your service. Your service should prepare the Specification it needs to build the query.
public interface YourRepository extends JpaRepository<HistoryBank, Long>,
JpaSpecificationExecutor<HistoryBank> {}
Your service can then use the aforementioned findAll method.
public PaginationList<HistoryBankWrapper, HistoryBank> findByFilter(String keyfilter,String sortField,String sortOrder, int page, int size) {
Sort sort = Sort.by(Sort.Direction.fromString(sortOrder), sortField);
Pageable paging = PageRequest.of(page, size);
Specification<HistoryBank> spec = (r, c, b) -> b.like(r.get(sortField), "%" + keyFilter + "%"));
Page<HistoryBank> historyPage = historyBankRepository.findAllFilter(spec, paging);
List<HistoryBank> historyList = historyPage.getContent();
List<HistoryBankWrapper> historyWrapperList = toWrapperList(historyList);
return new PaginationList<HistoryBankWrapper, HistoryBank>(historyWrapperList, historyPage);
}
You can even make this easier by modifying your controller to directly bind to a Pageable object, that saves you creating it.
public DataResponsePagination<HistoryBankWrapper, HistoryBank> pageFilter(
#RequestParam("filter") String keyfilter ,
Pageable page) {
{
Now you only need to create the Specification and can just pass along the Pageable.
In the Jpa repository you use the symbol of OR (||) try with a keyword or
and modified your query for filtering you have to use and instead of or
I am writing an REST API with Spring Boot.Here,I am trying to sort the list using the 'PagingAndSortingRepository'.This is what I have tried
Method in my Controller class
#GetMapping public ResponseEntity
<PagedModel<ComplainDTO>> getComplains(#RequestParam(defaultValue = "0",value = "page",required =false)int page, #RequestParam(value="sortBy" ,required = false,defaultValue = "complainId") String sortBy, PagedResourcesAssembler assembler) {
return ResponseEntity.status(HttpStatus.OK)
.body(assembler.toModel(complainService.getAllComplains(page,sortBy)));
}
Method on my service
#Override
public Page<ComplainDTO> getAllComplains(int page,String sortBy) {
Pageable pageable = PageRequest.of(page,20, Sort.by(sortBy));
Page<ComplainEntity> result =complainRepository.findAll(pageable);
return result.map(complainEntity -> toDTO(complainEntity));
}
I am taking input from the consumer to sort the list based on given attribute however the default behavior of Sort is Ascending .So,how can I implement this taking user params whether it being ascending or descending aside from using if-else.
The one option is asking the user to provide the sorting order using #RequestParam
#GetMapping public ResponseEntity
<PagedModel<ComplainDTO>> getComplains(#RequestParam(defaultValue = "0",value = "page",required =false)int page,
#RequestParam(value="sortBy" ,required = false,defaultValue = "complainId") String sortBy,
#RequestParam(value="orderBy" ,required = false,defaultValue = "ASC") String orderBy, PagedResourcesAssembler assembler) {
return ResponseEntity.status(HttpStatus.OK)
.body(assembler.toModel(complainService.getAllComplains(page,sortBy,orderBy)));
}
And then in the service pass the sorting order to Sort method
#Override
public Page<ComplainDTO> getAllComplains(int page,String sortBy, String orderBy) {
Pageable pageable = PageRequest.of(page,20, Sort.by(Sort.Direction.valueOf(orderBy),sortBy));
Page<ComplainEntity> result =complainRepository.findAll(pageable);
return result.map(complainEntity -> toDTO(complainEntity));
}
I have to write some REST service which should handle a million entries and return a response to the user in JSON format. I'm writing some REST-controller using Spring and make pagination.
public List<ContactDto> getAllContacts() {
double countItems = contactRepo.count();
int pages = (int) Math.ceil(countItems / totalItemsPerPage);
List<Contact> contacts = new ArrayList<>();
for (int i = 0; i < pages; i++) {
Page<Contact> page = contactRepo.findAll(PageRequest.of(i, totalItemsPerPage));
contacts.addAll(page.stream().collect(Collectors.toList()));
}
return contacts.stream()
.map(entity -> new ContactDto(entity.getId(), entity.getName()))
.collect(Collectors.toList());
}
I'm new in spring and pagination.
In this approach is have a sense or I'm doing something wrong?
I mean I want to know I'm using pagination write or wrong?
Thanks for the help!
It seems that you are collecting all the Contacts from all the pages and that does not make sense as you are storing all the data in memory negating all the lazy loading benefints.
I would suggest the following:
1.Rest controller should be able to accept pageNumber and pageSize arguments:
#GetMapping(value="/uri/{pageNumber}/{pageSize}")
public List<Contact> getContactsPage(#PathVariable("pageNumber") final Integer pageNumber, #PathVariable("pageSize") final Integer pageSize) {
//service or repository call
}
2.Repository interface should extent PagingAndSortingRepository:
public interface ContactRepository extends PagingAndSortingRepository<Contact, Long> {
Page<Contact> fingAll(Pageable pageable);
}
3.In your service or in controller directly create a Pageable object and pass it as ContactRepository#fingAll() argument:
final Pageable contactsPageable = PageRequest.of(pageNumber, pageSize);
4.Map Page to DTO if necessary.
You should take a look at Spring Data Rest.
I have an Angular based Application for Front End and Spring Boot for Back End.
My componaent had a variable that contains filters like that
search:any={
name:'',
surname:'',
address:'',
phone:'',
city:''
}
i pass it to my Api with Post Method like that:
getPeoplesByFilters(filter){
return this.http.post("http://localhost:8080/search", filter)
.map (resp => resp.json())
}
I receive this in my Spring Boot Controller by this method:
#RequestMapping(value="/search", method= RequestMethod.POST )
public Page<People> searchFilters(#RequestBody() People p,
#RequestParam(value="page", defaultValue="0") int page ,
#RequestParam(value="size", defaultValue="5") int size){
p.setName("%"+p.getName()+"%");
p.setSurname("%"+p.getSurname()+"%");
p.setAddress("%"+p.getAddress()+"%");
p.setPhone("%"+p.getPhone()+"%");
System.out.println(p.getName());
return poepleRepository.searchFilters( p , new PageRequest(page, size));
}
And in my Jpa Repository interface I have this query:
#Query("select p from poeple p where p.name like :#{#x.name} and p.surname like :#{#x.surname} and p.address like :#{#x.address} and p.phone like :#{#x.phone} and p.mobile like :#{#x.phone}")
public Page<People> searchFilters(#Param("x") People x, Pageable pageable);
But I don't have any result
I tried just with one field and i fill it in my front end App and it work
But when I fill just one field and let another empty i have zero result.
What's Wrong!
Thanks
My previous experience shows that using specifications is best for your case. Because it is more extendable and understandable. You can write specification like below.
public class Spec{
public static Specification<Person> filter(FilterParams filterParams) {
return (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if(filterParams.getName()!=null){
predicates.add(cb.equal(root.get("name"),filterParams.getName()));
}
if(filterParams.getSurname()!=null){
predicates.add(cb.equal(root.get("surname"),filterParams.getSurname()));
}
....
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
}
}
Then you can use like below:
#Service
public TestService{
#Autowired
PersonRepository personRepository;
void test(){
FilterParam filterParam = new FilterParam();
filterParam.setName("test);
...
personRepository.findAll(Spec.filter(filterParam));
}
}
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);
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());
There is a Page implementation for that:
Page<Something> page = new PageImpl<>(yourList);
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.
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.
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());
}
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.
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());
}
You can use this generic function for converting List to page.
public static<T> Page<T> convertToPage(List<T> objectList, Pageable pageable){
int start = (int) pageable.getOffset();
int end = Math.min(start+pageable.getPageSize(),objectList.size());
List<T> subList = start>=end?new ArrayList<>():objectList.subList(start,end);
return new PageImpl<>(subList,pageable,objectList.size());
}
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());
//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;
}
}
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());
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:
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());
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);
}
u didn't made paged result
new PageImpl<User>(users, pageable, users.size()); does not make paged result implicitly,
in this context, pageable argument just makes meta-data of Page object like page, offset, size... etc
So you have to use Repository method like
Page<User>findAllByProgramId(Integer programId, Pageable pageable);