Micronaut is a modern, JVM-based framework designed to help developers build fast, lightweight, and cloud-native applications. With the release of Java 21, Micronaut becomes even more powerful, thanks to the latest language features and improvements. In this post, we will walk through how to use Micronaut with Java 21 in a very simple and easy-to-understand way.

Why Micronaut and Java 21?
Before we dive into coding, let’s understand why Micronaut and Java 21 make a great combination:
Micronaut Benefits:
- Fast startup time: Great for microservices and serverless applications.
- Low memory footprint: Runs efficiently even on small servers.
- Dependency injection (DI): Built-in, compile-time DI for better performance.
- Cloud-native: Supports Kubernetes, serverless, and other modern deployment options.
Java 21 Benefits:
- Virtual Threads: Lightweight threads for highly concurrent applications.
- Pattern Matching: Easier and safer type checks.
- Record Classes: Simplifies data classes.
- Improved Performance and Security
Combining both gives you a powerful, modern development stack.
Prerequisites
Before starting, make sure you have the following installed:
- Java 21 JDK
- Micronaut CLI (optional but helpful)
- Install via SDKMAN:
sdk install micronaut
- Or download directly from Micronaut website.
- Install via SDKMAN:
- An IDE (such as IntelliJ IDEA, Eclipse, or VS Code)
Creating Your First Micronaut Project with Java 21
Step 1: Create the project
Using Micronaut CLI, run the following command:
1 2 3 |
mn create-app com.kscodes.micronaut21 --java-version 21 |
com.kscodes.micronaut21
is the package name.--java-version 21
ensures the project is set up for Java 21.
Alternatively, you can use the Micronaut Launch website to generate your project.
Step 2: Open the project in your IDE
- Import the generated project into your IDE.
- Make sure your IDE is using JDK 21.
Writing a Simple REST Endpoint
Let’s create a simple “Hello World” REST controller.
Step 1: Create a Controller
Create a new Java class:
File: src/main/java/com/kscodes/micronaut21/HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kscodes.micronaut21; import io.micronaut.http.annotation.*; @Controller("/hello") public class HelloController { @Get("/{name}") public String sayHello(String name) { return "Hello, " + name + "!"; } } |
@Controller
defines the base path.@Get
maps HTTP GET requests to the method.
Step 2: Run the application
Run the application using the gradle or maven bat
You can now test your endpoint by visiting:
http://localhost:8080/hello/World
You should see:
Hello, World!
Using Java 21 Features in Micronaut
Let’s use some Java 21 features to make our code modern.
Example 1: Using Record Classes
Create a record to hold greeting messages.
File: src/main/java/com/kscodes/micronaut21/Greeting.java
1 2 3 4 5 |
package com.kscodes.micronaut21; public record Greeting(String message) {} |
Update the controller to use this record:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kscodes.micronaut21; import io.micronaut.http.annotation.*; @Controller("/hello") public class HelloController { @Get("/{name}") public Greeting sayHello(String name) { return new Greeting("Hello, " + name + "!"); } } |
Now your endpoint returns a JSON response like:
1 2 3 4 5 |
{ "message": "Hello, World!" } |
Example 2: Using Virtual Threads (Preview)
Virtual threads allow you to handle many requests with fewer system resources.
In application.yml
, enable virtual threads:
1 2 3 4 5 6 |
micronaut: executors: io: type: virtual |
Micronaut will now use virtual threads for IO-bound tasks.
Testing the Application
Micronaut comes with built-in support for testing.
File: src/test/java/com/kscodes/micronaut21/HelloControllerTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.kscodes.micronaut21; import io.micronaut.test.extensions.junit5.annotation.MicronautTest; import org.junit.jupiter.api.Test; import jakarta.inject.Inject; import io.micronaut.http.client.HttpClient; import io.micronaut.http.client.annotation.Client; import static org.junit.jupiter.api.Assertions.*; @MicronautTest class HelloControllerTest { @Inject @Client("/") HttpClient client; @Test void testHelloEndpoint() { Greeting response = client.toBlocking().retrieve("/hello/World", Greeting.class); assertEquals("Hello, World!", response.message()); } } |
Summary
- We installed Java 21 and Micronaut.
- We created a simple Micronaut project.
- We built a REST API using Java 21 features like records and virtual threads.
- We wrote a simple test.
Micronaut with Java 21 gives you a modern, efficient, and highly performant platform for building microservices and cloud-native applications.