Java / Gradle

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.

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

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

Mark a bean as a controller

Annotate with @RestController and @RequestMapping

Annotate with @Controller

Identify a method as a GET endpoint

Annotate with @GetMapping

Annotate with @Get

Identify a method parameter as a path variable

Annotate with Spring’s @PathVariable

Annotate with Micronaut’s @PathVariable

Respond with an HTTP response that has a status code and a body

Return a ResponseEntity

Return an HttpResponse

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:

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

Micronaut Controller

This is the Micronaut controller:

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

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:

micronautframeworkjacksondatabind/java/src/main/java/example/micronaut/SaasSubscriptionController.java

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.

build.gradle
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.

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

Micronaut Test

The main difference from the Spring Boot test is that you use the Micronaut HTTP Client to test the embedded server.

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

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