5. Paginated List - Spring Boot vs Micronaut Framework - Building a REST API
This guide compares how to create a GET endpoint that returns a paginated JSON Array in Micronaut and Spring Boot applications.
On this guide
In this section
Sample Project
You can download a sample application with the code examples in this article.
Introduction
This guide compares how to write a GET endpoint that returns a paginated list backed by a persistence layer in Micronaut Framework and Spring Boot applications.
The Spring Boot application uses Spring Data and H2. The Micronaut application uses Micronaut Data and H2. Micronaut Data is a database access toolkit that uses Ahead of Time (AoT) compilation to pre-compute queries for repository interfaces that are then executed by a thin, lightweight runtime layer.
This guide is the fifth tutorial of Building a REST API - a series of tutorials comparing how to develop a REST API with Micronaut Framework and Spring Boot.
Pagination
When returning multiple records you need some control over paging the data. Micronaut Data includes the ability to specify pagination requirements with the Pageable type. The same concept exists in Spring Data with org.springframework.data.domain.Pageable.
Repository
Micronaut Data and Spring Data enhance the repository pattern by providing paginated interfaces from which you can extend.
Spring Data package is org.springframework.data.repository and Micronaut Data package is io.micronaut.data.repository.
| Spring Data | Micronaut Data | |
|---|---|---|
Create, Read, Update, Delete Operations |
|
|
Paginated Operations |
|
|
Spring Boot
The repository in the Spring Boot application extends both CrudRepository and PagingAndSortingRepository.
Micronaut
The repository in the Micronaut application extends PageableRepository.
Controller
Both frameworks simplify the creation of paginated endpoints by allowing the binding of Pageable as a controller method parameter.
Spring Boot Controller
In the Spring Boot application, we build a PageRequest, a basic Java Bean implementation of Pageable, with the Pageable parameter and then pass it to the repository.
Micronaut Controller
In the Micronaut application, we pass a Pageable instance to the repository.
Tests
In this tutorial, we use AssertJ in the tests. Moreover, we use Jayway JsonPath - a Java DSL for reading JSON documents.
In this tutorial, we use Json-smart in the tests.
Json-smart is a performance focused, JSON processor lib.
See Data
The seed data used in the tests is the same for both applications. We modified it to contain multiple entries.
INSERT INTO saas_subscription(id, name, cents) VALUES (99, 'Advanced', 2900);
INSERT INTO saas_subscription(id, name, cents) VALUES (100, 'Essential', 1400);
INSERT INTO saas_subscription(id, name, cents) VALUES (101, 'Professional', 4900);Spring Boot Test
The following tests verify the following scenarios:
-
Returning a list specifying the sorting
-
Returning a list with the default sorting
-
Returning a list specifying no pageable parameters
Micronaut Test
The Micronaut Test is almost identical to the previous Spring Boot test. The main difference is that the Micronaut Test uses the built-in HttpClient instead of TestRestTemplate and HttpResponse instead of ResponseEntity.
Conclusion
Adding pagination to the persistence layer is easy in both frameworks, and the API is almost identical. However, Micronaut Data’s compile-time/reflection-free approach results in better performance, smaller stack traces, and reduced memory consumption.
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |