4. Data - Spring Boot vs Micronaut Framework - Building a REST API
This guide compares how to add Spring Data or Micronaut Data to power a GET endpoint in both 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 power a GET endpoint with 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.
This guide is the fourth tutorial of Building a REST API - a series of tutorials comparing how to develop a REST API with Micronaut Framework and Spring Boot.
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.
Micronaut Data vs Spring Data
Micronaut Data improves on Spring Data in the following ways:
-
No runtime model - Spring Data maintains a runtime metamodel that uses reflection to model relationships between entities. This model consumes significant memory and memory requirements grow as your application size grows. The problem is worse when combined with Hibernate which maintains its own metamodel as you end up with duplicate meta-models.
-
No query translation - Spring Data uses regular expressions and pattern matching in combination with runtime generated proxies to translate a method definition on a Java interface into a query at runtime. No such runtime translation exists in Micronaut Data and this work is carried out by the Micronaut compiler at compilation time.
-
No Reflection or Runtime Proxies - Micronaut Data uses no reflection or runtime proxies, resulting in better performance, smaller stack traces and reduced memory consumption due to a complete lack of reflection caches (Note that the backing implementation, for example Hibernate, may use reflection).
-
Type Safety - Micronaut Data will actively check at compile time that a repository method can be implemented and fail compilation if it cannot.
Dependencies
Spring Boot Data and H2
To use Spring Data JDBC and H2 add the following dependencies:
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
runtimeOnly("com.h2database:h2")Micronaut Data and H2
To use Micronaut Data JDBC and H2 add the following dependencies:
annotationProcessor("io.micronaut.data:micronaut-data-processor")
implementation("io.micronaut.data:micronaut-data-jdbc")
runtimeOnly("com.h2database:h2")Please note the addition of the micronaut-data-processor in the annotation processor classpath. Micronaut Data does a lot of work at compilation, which leads to applications with better performance and a reduced memory consumption.
Entities
We modified the SaasSubscription Java Record. We use it as a persistence entity.
Spring Boot
For Spring Data, we added the @Id annotation.
package example.micronaut;
import org.springframework.data.annotation.Id;
record SaasSubscription(@Id Long id, String name, Integer cents) {
}Micronaut
For Micronaut Framework, the entity is annotated with @MappedEntity.
SQL
Spring Boot loads SQL from the standard root classpath locations: schema.sql and data.sql. Micronaut Framework recommends you manage your database schema with Liquibase or Flyway.
However, to keep applications as close as possible, you will create the database schema with an SQL file in the Micronaut application. Micronaut Test supports loading SQL before tests seamlessly.
We will use two SQL files in the tests:
-
src/main/resources/schema.sql. -
src/main/resources/data.sql.
SQL Schema
CREATE TABLE IF NOT EXISTS saas_subscription
(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(255) NOT NULL,
cents NUMBER NOT NULL DEFAULT 0
);Sample Data
INSERT INTO saas_subscription(id, name, cents) VALUES (99, 'Advanced', 2900);Repository
Both Micronaut Data and Spring Data enable developers to use the repository pattern. You write an interface and the framework provides the implementation.
Spring Boot Repository
package example.micronaut;
import org.springframework.data.repository.CrudRepository;
interface SaasSubscriptionRepository extends CrudRepository<SaasSubscription, Long> {
}Micronaut Repository
Because Micronaut Data works at build-time, you have to specify the dialect.
Controller
We modified the controller we wrote in the previous tutorial to inject the repository. We use constructor injection in both applications.
Spring Boot Controller
Micronaut Controller
Tests
In this tutorial, we use AssertJ in the tests. Moreover, we use Jayway JsonPath - a Java DSL for reading JSON documents.
Micronaut Test
The Spring Boot tests are identical to those in the previous tutorial. In the Micronaut test, you need to annotate the test with @Sql to load the SQL files before the tests.
Conclusion
Adding a 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). |