An introduction about Micronaut

I introduced you to Quarkus, a Java framework that makes it possible to build Java Cloud Native applications, reducing application start time and reducing the memory footprint used to run applications. Micronaut is also one of the frameworks that allow us to do this. Combined with GraalVM, Micronaut can help us build native images and run on Cloud providers with containers. In this tutorial, I will give you an overview of Micronaut!


Create new project

Similar to Spring or Quarkus, Micronaut also provides us with a tool to generate projects with Micronaut easily. It’s Micronaut Launch at: https://micronaut.io/launch/.

There are many Application Types that Micronaut supports, but for the sake of simplicity for the example of this tutorial, I will create a new web application with Micronaut, so I will choose the Application Type as Micronaut Application.

Similar to Spring Initializr, we can preview how the project will be generated, including what dependencies.

To add dependencies or features to use for the application, click Features:

There are many frameworks and libraries supported by Micronaut. There is also support for the Spring framework!

But since I’m just creating a basic web app, I won’t need to choose any features! By default, Micronaut already supports it.

I will configure the project as follows:

then click Generate to download the project, then import it into IntelliJ.

The following results:




Run application

By default, Micronaut uses the Netty server to run the application. Now, if you open the Application class and run it, then go to http://localhost:8080/, you will see the following results:

This is because we have not defined any requests for our application yet.

You can define a “/hello” request with Micronaut as follows:

As you can see, Micronaut also has its own annotations that support building web applications, similar to the Spring framework, so if you are familiar with the Spring framework, working with Micronaut is easy.

The result when I run the application again and then request to http://localhost:8080/, as follows:

If you look at IntelliJ’s Console, you will see the application start time as follows:

Now, let’s try to build a native image with this application and see how the start time is!

Please open Terminal or Console and go to the project folder, then run the following command:

Note that you need to install the native-image component using GraalVM and must set the JAVA_HOME environment variable to point to GraalVM’s Java installation directory.

With the current version of Micronaut, there is a NoClassDefFoundError error:

you need to add javax.inject dependency to pom.xml file:

After the build is successful, go to the target directory, then run the application with the following command:

The following results:

14ms, it’s excellent, isn’t it? Almost instantly 😀

Add Comment