3. POST - Spring Boot vs Micronaut Framework - Building a REST API
This guide compares how to add POST 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 write a POST endpoint 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 third tutorial of Building a REST API - a series of tutorials comparing how to develop a REST API with Micronaut Framework and Spring Boot.
Controller
The controller receives a POST request with a JSON body, the repository persists the SaasSubscription, and then it returns
a 201 response with a Location HTTP header.
As you will see, both controllers are almost identical, with similar annotations, as we discussed in the second guide of this series.
The new APIs used in the following controllers are similar:
| Spring Boot | Micronaut | |
|---|---|---|
Identify a method as a POST endpoint |
|
|
Read and deserialize the request body to a method parameter |
|
|
Fluent API to build a URI |
|
|
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.
The test saves a subscription and then uses the response Location header to fetch the saved subscription. It then compares if the GET response matches what we sent in the POST request.
Spring Boot Test
The following test shows that retrieving the status code or the location header from a ResponseEntity is easy.
Micronaut Test
The following test shows that retrieving the status code or the location header from an HttpResponse is easy.
Conclusion
As you see in this guide, the code to write a POST endpoint backed by a persistence layer in Micronaut Framework and Spring Boot applications is very similar. However, the Micronaut compile-time approach does not require reflection or proxies, leading to faster and more efficient applications.
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). |