[SOLVED] Spring Boot Pagination returning null array – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Amit Dev

Trying my hands at Spring Boot Pagination. But the resulting array is empty.

Controller:

package mypackage;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @Autowired
    CustomerRepository customerRepository;
    
    @GetMapping(value="/get/customers/{page}")
    public List<Customers> getCustomersPaginated(@PathVariable("page") int page)
    {
        Pageable pageableObject=PageRequest.of(page, 2, Sort.by("cnum").ascending());
        Page<Customers> pageObject = customerRepository.findAll(pageableObject);
        return pageObject.getContent();
    }
}

CustomerRepository:

package mypackage;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerRepository extends JpaRepository<Customers, Integer> {

}

Customers:

package mypackage;
import javax.persistence.Entity;
import javax.persistence.Id;

import lombok.Data;
import lombok.RequiredArgsConstructor;

@Entity//(name = "customers")
@Data
@RequiredArgsConstructor
public class Customers {
    
    @Id
    int cnum;
    int rating,snum;
    String cname, city;

}

Customers Table schema:

mysql> desc customers;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| cnum   | int         | NO   | PRI | NULL    |       |
| cname  | varchar(20) | NO   |     | NULL    |       |
| city   | varchar(20) | NO   |     | NULL    |       |
| rating | int         | NO   |     | NULL    |       |
| snum   | int         | YES  | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+

But the resulting array is empty, as shown in the following screenshot:

enter image description here

What do you think is the problem? It knows that I am requesting 2 results per page, but it is sending an array of null records. What am I missing here?



Solution

the query you are getting in logs , in the end there is limit ?,? which looks incorrect it should be limit ? offset ? when you are passing value of page other than 0

as for count query it gets executed when you don’t have offset but since you are passing 1 for page than there should be offset and count query should not get executed , can you share you pom.xml need to check the version of dependencies you are using


This Question was asked in StackOverflow by Amit Dev and Answered by Anurag Negi It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?