Spring – REST Pagination
Spring Framework is built on top of servlets. This particular web framework comes with very essential features with which we can develop efficient and effective web applications. On top of Spring Framework, Spring Boot was released in April 2014. The main aim behind the Spring Boot was the feature of Auto-Configuration. Spring MVC ( Model View Controller ) is the sub-domain or sub-project of the Spring Framework which is embedded in Spring’s ‘Web Starter’ dependency. It is used for developing web applications. Web Starter also contains features for developing REST API. We use it to return the data or list of data instead of the HTML pages.
The data which is retrieved from a data source can be very large that it will be inconvenient to list all of them, that too directly. This retrieved or returned data can be controlled in a number of ways. For example, to limit the entities or objects return, the way they are sorted, etc. Here, Pagination comes into action.
Features of the Pagination
- Limit output to the respective set of items.
- Can traverse from one set of items to another set.
- Can handle the large offsets of data values.
- The output can be ordered. For eg – Ascending or Descending according to some property.
Working of Pagination using Spring’s REST API
A. File: pom.xml (Configuration of the application)
C. application.properties ( Configuration of the Properties )
- This file contains all the properties and their values that are required to build a Datasource object.
- The Datasource object is provided by the ‘Spring Data JPA’ dependency.
- The Spring Data JPA has the default implementation of the Hibernate.
- This file also contains some Hibernate properties.
- The Datasource object built is then used by the MySQL Driver to connect to the database.
- Therefore, we can also interact with the respective database.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/userdetails spring.datasource.username=root spring.datasource.password=password spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
This class is mapped to a table in a database.
1. Persistence Annotations:
- @Entity – It specifies that the class is an Entity. The ‘name’ attribute is used to map the Class name to the table name in the database if they are not the same.
- @Id – It specifies that the property it is applied on acts as a primary key for the respective table with which we can access the database.
2. Lombok Annotations
- @Data – It automatically generates Getter and Setter methods for all the fields at runtime.
- @RequiredArgsConstructor – It generates constructors for all required fields i.e The fields with keyword final or @NonNull annotation. If no such fields are present then a zero arguments constructor is created. This annotation is a part of @Data annotation.
The object of this class will be returned as a JSON response by the REST API.
E. File: UserRepository.java
- This interface is used to work with databases without the need to write the cumbersome JDBC ( Java Database Connectivity ) methods.
- This interface extends ‘Interface JpaRepository<T, ID>’, Where T is an Object type and ID is the type of a primary key.
Note: JpaRepository has 4 superinterfaces QueryByExampleExecutor<T>, Repository<T,ID>, CrudRepository<T,ID> and PagingAndSortingRepository<T, ID> which is the one that provides essential Pagination features.
CrudRepository provides the generalized JDBC methods which run SQL scripts to interact with the databases.
(Create, Read, Update, Delete).
public interface PagingAndSortingRepository<T,ID> extends CrudRepository<T,ID>
Methods of PagingAndSortingRepository interface are as follows:
|Page<T> findAll(Pageable pageable)||Returns a Page of entities meeting the paging restriction provided in the Pageable object.|
|Iterable<T> findAll(Sort sort||Returns all entities sorted by the given options.|
F. File: PagedRestController.java
- This controller class returns a list of UserEntities objects as a response.
- Here, a Pageable object is created using PageRequest.of() factory method.
- This method accepts 3 arguments:
- A number of a respective page.
- Number of entries to retrieve per page.
- Sorting technique.
This object of Pageable is passed to the findAll() method of PagingAndSortingRepository<T, ID> which accepts the same respective object and returns a Page object. Then the items retrieved from the Page object are eventually returned.
- As most industries work with large datasets, REST APIs are very popular and widely used as they help in returning data responses.
- Therefore, for them, Pagination becomes a very crucial feature for them to apply.
- It also helps to build a good and manageable UI ( User Interface).
Please Login to comment...