Java / Gradle

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.

Sergio del Amo
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.

Table 1. Comparison between Spring Boot and Micronaut Framework
Spring Data Micronaut Data

Create, Read, Update, Delete Operations

CrudRepository

CrudRepository

Paginated Operations

PagingAndSortingRepository

PageableRepository

Spring Boot

The repository in the Spring Boot application extends both CrudRepository and PagingAndSortingRepository.

springboot/java/src/main/java/example/micronaut/SaasSubscriptionRepository.java

Micronaut

The repository in the Micronaut application extends PageableRepository.

micronautframework/java/src/main/java/example/micronaut/SaasSubscriptionRepository.java

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.

springboot/java/src/main/java/example/micronaut/SaasSubscriptionGetListController.java

Micronaut Controller

In the Micronaut application, we pass a Pageable instance to the repository.

micronautframework/java/src/main/java/example/micronaut/SaasSubscriptionGetListController.java

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.

springboot/java/src/test/resources/data.sql
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

springboot/java/src/test/java/example/micronaut/SaasSubscriptionGetListControllerTest.java

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.

micronautframework/java/src/test/java/example/micronaut/SaasSubscriptionGetListControllerTest.java

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).