Micronaut with VS Code: Recommended Setup

Micronaut is a modern, lightweight Java framework. Many developers love using VS Code because it’s free, fast, and full of useful extensions. In this guide, we will go step-by-step to set up Micronaut development in Visual Studio Code (VS Code) in a simple and beginner-friendly way.

Micronaut with VS Code

Why Use Micronaut with VS Code?

  • Lightweight IDE: Faster startup compared to heavy IDEs.
  • Great extensions: Supports Java, Micronaut, debugging, and more.
  • Cross-platform: Works on Windows, Mac, and Linux.
  • Free and open-source

Combining Micronaut and VS Code makes microservice development smooth and efficient.


Prerequisites

Before starting, make sure you have:

  1. Java 21 JDK
  2. Micronaut CLI (Optional but useful)
  3. Visual Studio Code

Step 1: Install Required VS Code Extensions

Open VS Code and go to the Extensions panel (left sidebar).

Search and install these extensions:

Extension NamePurpose
Java Extension PackProvides Java support, debugging, Maven, Gradle
Micronaut ToolsAdds Micronaut-specific support
Gradle for JavaGradle support for builds
Docker (optional)If you plan to containerize apps

You can also install the Java Extension Pack directly:


ext install vscjava.vscode-java-pack

Step 2: Create a New Micronaut Project

Using Micronaut Launch

  1. Go to Micronaut Launch.
  2. Choose:
  3. Generate and download the zip file.
  4. Extract it and open the folder in VS Code.

Using Micronaut CLI (optional)

If you installed Micronaut CLI:


mn create-app com.kscodes.micronautvscode --java-version 21

Open the generated folder in VS Code:


code com.kscodes.micronautvscode

Step 3: Configure Java in VS Code

VS Code should automatically detect Java. If not:

  1. Go to Settings > Java: Home.
  2. Set it to your Java 21 JDK path.

Step 4: Build and Run Your Micronaut App

Build with Gradle

Use the integrated terminal (Ctrl+`) in VS Code:


./gradlew build

Run the App


./gradlew run

Alternatively, you can use VS Code’s Run and Debug option for better control.


Step 5: Create Your First Controller

Let’s create a simple Hello World REST API.

File: src/main/java/com/kscodes/micronautvscode/HelloController.java


package com.kscodes.micronautvscode;

import io.micronaut.http.annotation.*;

@Controller("/hello")
public class HelloController {

    @Get("/{name}")
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

Test it by opening:

http://localhost:8080/hello/World

You should see:


Hello, World!

Step 6: Debugging in VS Code

  1. Open the Run and Debug panel.
  2. Click create a launch.json file.
  3. Select Java as environment.
  4. VS Code will generate the config.
  5. Click Run to start debugging.

Step 7: Enable Virtual Threads (Java 21 Feature)

In application.yml:


micronaut:
  executors:
    io:
      type: virtual

This allows Micronaut to use Java 21 virtual threads.


Step 8: Running Tests

Micronaut supports JUnit 5 out of the box.

File: src/test/java/com/kscodes/micronautvscode/HelloControllerTest.java


package com.kscodes.micronautvscode;

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() {
        String response = client.toBlocking().retrieve("/hello/World");
        assertEquals("Hello, World!", response);
    }
}

Run tests using VS Code’s Testing panel.


Summary

  • Install Java 21, Micronaut CLI, and VS Code.
  • Add Java and Micronaut extensions to VS Code.
  • Create, build, run, and debug Micronaut apps directly inside VS Code.
  • Leverage Java 21 features like virtual threads.

Useful Links


Stay tuned for more beginner-friendly Micronaut tutorials!