Java / Gradle

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.

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 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:

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

Identify a method as a POST endpoint

@PostMapping

@Post

Read and deserialize the request body to a method parameter

@RequestBody

@Body

Fluent API to build a URI

UriComponentsBuilder

UriBuilder

Spring Boot Controller

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

Micronaut Controller

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

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.

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

Micronaut Test

The following test shows that retrieving the status code or the location header from an HttpResponse is easy.

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

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