Using Micronaut with Java 21

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.

Using Micronaut with Java 21

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:

  1. Java 21 JDK
  2. Micronaut CLI (optional but helpful)
    • Install via SDKMAN:sdk install micronaut
    • Or download directly from Micronaut website.
  3. 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:

  • 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

  • @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

Update the controller to use this record:

Now your endpoint returns a JSON response like:

Example 2: Using Virtual Threads (Preview)

Virtual threads allow you to handle many requests with fewer system resources.

In application.yml, enable virtual threads:

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

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.