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:
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?
Edit 1: Eclipse logs. Why is it firing a query for count?
Edit 2: pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringPagination</groupId>
<artifactId>SpringPagination</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
CodePudding user response:
Pagination starts zero. Make sure you have enough rows. If there is any row, send page 0 and check if something is coming.
You can try:
PageRequest.of(0, 2);
or
For more detailed information on how to paginate, you can review.
CodePudding user response:
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