Atharva Siddhabhatti
Atharva Siddhabhatti's Blog

Atharva Siddhabhatti's Blog

Spring Boot - Pagination and Sorting Project

Spring Boot - Pagination and Sorting Project

Atharva Siddhabhatti's photo
Atharva Siddhabhatti
·Oct 17, 2021·

2 min read

Pagination is often helpful when we have a large dataset and we want to present it to the user in smaller chunks. Also, we often need to sort that data by some criteria while paging.


1) Clone the Repo from here CLICK HERE

2) Import Maven based project in any of your Favourite IDE.

./mvnw spring-boot:run


Open in Browser

Access H2 Database.



localhost:8080/student/searchGET-To get all students in paginated format
localhost:8080/studentPOST{ "id":214214,"name":"Atharva","rollno":32 }To Add Students to the DB
localhost:8080/pagination?pageSize=5GET-to get first 5 student details
localhost:8080/pagination?pageSize=5&pa..GET-To get student details by pagesize 5 and pageno 2
localhost:8080/pagination?pageSize=5&pa..GET-To get student details by pagesize 5 and pageno 1
localhost:8080/pagination?pageSize=5&pa..GET-To sort student details by id
localhost:8080/pagination?pageSize=5&pa..GET-To sort student by name


public interface SortingRepository extends PagingAndSortingRepository<Student, Integer>  {


public PaginatedStudentResponse readStudent(Pageable pageable) {
        Page<Student> students = studentRepository.findAll(pageable);
        return PaginatedStudentResponse.builder()


    public List<Student> getAllStudents(Integer pageNo, Integer pageSize, String sortBy)
            Pageable paging = PageRequest.of(pageNo, pageSize,;
            Page<Student> pageResult = studentRepository.findAll(paging);

            if(pageResult.hasContent()) {
                return pageResult.getContent();
                return new ArrayList<Student>();

    public Student createStudent(Student student) {

    public ResponseEntity<List<Student>> getAllStudents(
            @RequestParam(defaultValue = "0") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "id") String sortBy)
        List<Student> list = service.getAllStudents(pageNo, pageSize, sortBy);

        return new ResponseEntity<List<Student>>(list, new HttpHeaders(),HttpStatus.OK);

    private StudentService studentService;

    public ResponseEntity<PaginatedStudentResponse> readStudent(Pageable pageable) {
        return ResponseEntity.ok(studentService.readStudent(pageable));

    public ResponseEntity<Student> createProduct(@RequestBody Student student){
        return ResponseEntity.ok().body(this.studentService.createStudent(student));

Configuration = true

# Enabling H2 Console

# Enable Web Access


Did you find this article valuable?

Support Atharva Siddhabhatti by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this