Do you like Spring Boot? It can be better, faster and with a smaller memory footprint! Take a look at the modern, JVM based Micronaut framework. In this post we will get started and create an API in Kotlin.

For more reading on Micronaut go to their website here. Or go to the documentation, here organised by subject.

We are going to create a backend for the android app we created in a previous post. The goal would be an API with one endpoint returning all new garden-planner actions. The complet code can be found on github here. Here we go!

Setup Micronaut

Micronaut has a nice cli that gets you up and running fast. Go here to see how to install, for me it was

brew install micronaut

Now we can use the cli to get the skeleton for our new api.

mn create-app garden-planner-api --lang=kotlin

Take a look at what we got there in just seconds. We got a gradle build, an application setup and a Dockerfile for free. Nice!

The Micronaut application

For simplicity we will copy the data model we had before.

data class Action(val title: String, val code: UUID, val description: 
    String, val type: ActionType)

enum class ActionType {
        ADDITION,
        MAINTENANCE,
        ALERT
}

We will expose static test data from a repository just to ignore the database details. We just jump to the controller exposing the actual endpoint.

@Controller("/actions")
class ActionController(val actionRepository: ActionRepository) {

    @Get("/")
    fun getActions(): Single<List<Action>> {
        return actionRepository.fetchActions()
    }
}

This feels really familiar coming from Spring Boot. With a few annotations we specify the controller and the endpoint. Let’s run it.

./gradlew build
docker build -t garden-planner-api .
docker run -p 8080:8080 garden-planner-api
curl http://localhost:8080/actions

Did you notice how fast it was ready to go in comparison to a Spring Boot application? This is one of the powers of Micronaut: what Spring puts together at runtime using reflection, Micronaut does at compile time.

Evaluation

We already saw the fast startup time thanks to the compile time dependency injection. The docker image we ran is 123MB big which is not big. We could also run the fat jar.

./gradlew shadowJar
java -jar webapp/build/libs/webapp-0.1-all.jar

This jar is just 20,2MB big and run the entire application. This is why Micronaut applications are very well adapted for microservices with its tiny memory footprint.

Hopefully you are now able to create your own API using the Micronaut framework. Happy nauting!