Micronaut Hello World Example
Micronaut is a modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications. Let us delve into understanding this Micronaut Hello World example to get started with the framework.
1. What is Micronaut?
Micronaut is a modern, JVM-based, full-stack framework specifically designed for building modular, high-performance microservices, serverless applications, and cloud-native systems. It offers first-class support for Java, Kotlin, and Groovy, making it a flexible choice for JVM developers.
Unlike traditional Java frameworks that rely heavily on runtime reflection and proxies (such as Spring), Micronaut uses compile-time annotation processing for dependency injection, AOP (Aspect-Oriented Programming), and configuration. This unique approach drastically reduces memory usage and startup time, making it ideal for microservices and serverless environments like AWS Lambda or Google Cloud Functions.
Micronaut also integrates seamlessly with GraalVM to enable ahead-of-time (AOT) compilation into native executables, further improving performance and resource efficiency.
Key features of Micronaut include:
- Compile-time dependency injection: No reflection or runtime proxies, leading to better performance and smaller memory footprint.
- Fast startup time and low memory usage: Ideal for microservices and containerized environments.
- Seamless integration with GraalVM: Supports building native images for ultra-fast execution and minimal memory use.
- Reactive and non-blocking: Includes built-in support for reactive programming using RxJava, Project Reactor, etc.
- Built-in HTTP server/client: Lightweight Netty-based web server and declarative HTTP clients with easy configuration.
- Cloud-native readiness: Support for service discovery, distributed tracing, configuration management, and more.
- Modular architecture: Encourages separation of concerns and better maintainability.
1.1 What is Micronaut CLI?
The Micronaut CLI (Command Line Interface) is a development tool that simplifies the process of creating and managing Micronaut applications. It provides a quick and efficient way to bootstrap projects, generate application components, and execute various tasks — all from your terminal.
It comes with intelligent code generation capabilities, which reduces the boilerplate and enforces consistent project structure. This makes it especially helpful for teams or individuals working on multiple services within a microservices architecture.
The CLI allows you to:
- Generate new Micronaut projects: Quickly scaffold applications with the desired language (Java/Kotlin/Groovy), test framework, and build tool (Gradle/Maven).
- Create application components: Generate controllers, services, repositories, and other common artifacts with ease.
- Run the application locally: Use the built-in development server to test your application quickly.
- Build and test: Compile your app, run tests, or create executable JARs directly using CLI commands.
- Manage features: Enable or disable features (e.g., security, metrics, database integration) during project creation.
Whether you’re a solo developer or part of a larger team, the Micronaut CLI can significantly improve your productivity by automating repetitive tasks and reducing manual errors. You can also explore the Micronaut Documentation for more advanced CLI usage.
2. Setting up Micronaut Project
Micronaut provides a CLI, but you can also generate a project using Micronaut Launch. For this article, we will use the CLI to create our project.
2.1 Install Micronaut CLI and Create a New Project
To get started with Micronaut, you’ll first need to install the Micronaut CLI. The CLI allows you to scaffold projects, create components, and manage builds with ease. If you have SDKMAN! installed, use the following command:
sdk install micronaut
This command downloads and installs the latest version of the Micronaut CLI. Once the installation is complete, you can create a new Micronaut project using the CLI. For example, to create a simple “Hello World” application using Java and Gradle as the build tool, run:
mn create-app com.example.helloworld --build=gradle --lang=java
Here’s a breakdown of the command:
com.example.helloworld
is the base package and project name.--build=gradle
specifies the use of Gradle as the build tool. You can also usemaven
if preferred.--lang=java
sets Java as the programming language. Micronaut also supportskotlin
andgroovy
.
After running this command, Micronaut will generate a fully structured project with a ready-to-run setup, including build scripts, configuration files, and a basic application structure.
3. Code Example
3.1 Understanding the Dependencies
Let’s walk through the key dependencies that are added to the build.gradle
file of our Micronaut application. These dependencies are essential for building and running a basic RESTful service using Micronaut.
plugins { id "io.micronaut.application" version "4.0.0" id "java" } micronaut { runtime("netty") testRuntime("junit5") processing { incremental(true) annotations("com.example.helloworld.*") } } dependencies { implementation("io.micronaut:micronaut-runtime") implementation("io.micronaut:micronaut-http-server-netty") implementation("io.micronaut:micronaut-inject") implementation("jakarta.annotation:jakarta.annotation-api") runtimeOnly("ch.qos.logback:logback-classic") testImplementation("io.micronaut:micronaut-http-client") testImplementation("org.junit.jupiter:junit-jupiter-api") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") }
This Gradle build file sets up a Micronaut application using the io.micronaut.application
plugin (version 4.0.0) along with Java support. The application is configured to run on the Netty server and use JUnit 5 as the test runtime. Annotation processing is enabled incrementally for classes under the com.example.helloworld.*
package. The dependencies include core Micronaut libraries like micronaut-runtime
, micronaut-http-server-netty
, and micronaut-inject
, as well as jakarta.annotation-api
for standard annotations. For runtime logging, logback-classic
is included. Testing dependencies include micronaut-http-client
for making HTTP requests in tests, junit-jupiter-api
for writing unit tests, and junit-jupiter-engine
for running them.
3.2 Creating a Default Page and Hello World Controller
Inside src/main/java/com/example/helloworld
, create a new file:
package com.example.helloworld; import io.micronaut.http.annotation.Controller; import io.micronaut.http.annotation.Get; @Controller("/") // root URL public class HelloWorldController { @Get("/") // GET request to "/" public String index() { return "Hello, World!"; } @Get("/greet/{name}") public String greet(String name) { return "Hello, " + name + "!"; } }
This Java class defines a simple Micronaut controller mapped to the root URL path (@Controller("/")
). It contains two HTTP GET endpoints: the first method, index()
, responds to GET requests at the root path (@Get("/")
) and returns the plain text “Hello, World!”. The second method, greet(String name)
, handles GET requests to /greet/{name}
, where {name}
is a dynamic path variable, and returns a personalized greeting in the format “Hello, [name]!”.
3.3 Understanding the application.yml File
Micronaut uses application.yml
as the default configuration file format, similar to Spring Boot. It allows you to define application settings like server port, environment profiles, logging levels, etc.
micronaut: application: name: helloworld netty: default: allocator: max-order: 3 logger: levels: io.micronaut: INFO
This YAML configuration file sets up a Micronaut application named helloworld
. It configures the Netty server’s memory allocator by setting the max-order
value to 3
, which controls the maximum chunk size of allocated memory. Additionally, it sets the logging level for the io.micronaut
package to INFO
, ensuring that informational messages and higher severity logs are captured during application execution.
3.4 Understanding Application.java
The Application.java
file serves as the main entry point of your Micronaut application. This is where the application lifecycle starts, similar to the main()
method in any standard Java application.
package com.example.helloworld; import io.micronaut.runtime.Micronaut; public class Application { public static void main(String[] args) { Micronaut.run(Application.class, args); } }
4. Running the Application
You can run the Micronaut application using Gradle with the following command:
./gradlew run
Once the application starts successfully, open your browser or use tools like cURL or Postman to test the API endpoints.
-- Request 1 curl http://localhost:8080/ -- Output 1 Hello, World! -- Request 2 curl http://localhost:8080/greet/John -- Output 2 Hello, John!
5. Conclusion
In this article, we built a simple “Hello World” application using the Micronaut framework. We began by setting up a new project via the CLI, created a basic controller to respond to HTTP requests, and ran the application to see the output. We also explored the structure of the application.yml
configuration file and reviewed the dependencies included in the project. Micronaut’s fast startup time, compile-time dependency injection, and low memory usage make it an excellent choice for modern microservices and serverless applications. With this foundational knowledge, you are now ready to expand your application by adding more endpoints, integrating external systems like databases or message queues, and managing multiple environments with tailored configurations. Micronaut offers a clean and efficient developer experience that scales well in production environments—happy coding!