2. Implementing GET - Spring Boot vs Micronaut Framework - Building a REST API
This guide compares how to implement a GET endpoint with Micronaut Framework and Spring Boot.
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 implement a GET endpoint in Micronaut Framework and Spring Boot applications.
This guide is the second tutorial of Building a REST API - a series of tutorials comparing how to develop a REST API with Micronaut Framework and Spring Boot.
Controller
To implement a GET /subscriptions/{id} endpoint, you need to create a controller in both frameworks.
The API is quite similar:
| Spring Boot | Micronaut | |
|---|---|---|
Mark a bean as a controller |
Annotate with |
Annotate with |
Identify a method as a GET endpoint |
Annotate with |
Annotate with |
Identify a method parameter as a path variable |
Annotate with Spring’s |
Annotate with Micronaut’s |
Respond with an HTTP response that has a status code and a body |
Return a |
Return an |
|
Note
|
Another important difference is the controller’s method visibility. Micronaut Framework does not use reflection (which leads to better performance and better integration with technologies such as GraalVM). Thus, it requires the controller’s methods to be public, protected, or package-private (no modifier). Throughout these tutorials, Micronaut controllers' methods use package-private. |
Spring Boot Controller
This is the Spring Boot controller:
Micronaut Controller
This is the Micronaut controller:
Without HttpResponse
|
Note
|
The default HTTP Status code in a Micronaut controller method is 200. However, when a Micronaut controller’s method returns null, the application responds with a 404 status code. |
Thus, you could simplify the previous controller as:
Route Compile-Time Validation
The Micronaut framework supports validating route arguments at compile time. Route arguments will automatically be checked at compile time if your application contains the following dependency.
annotationProcessor("io.micronaut:micronaut-http-validation")This enables an early feedback loop. The earlier you catch errors, the less time you spend debugging, the less money it costs you to develop an application.
For example, if you replace the @Get("/{id}") annotation with @Get("/{identifier}"), the application fails to compile.
Tests
In this tutorial, we use AssertJ in the tests. Moreover, we use Jayway JsonPath - a Java DSL for reading JSON documents.
Spring Boot Test
We could write a test in Spring Boot using TestRestTemplate.
Micronaut Test
The main difference from the Spring Boot test is that you use the Micronaut HTTP Client to test the embedded server.
Conclusion
Defining routes is extremely similar in both frameworks. However, Micronaut Framework provides compile-time validation of routes and a reflection-free approach to do it.
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). |