Hello World: Your First Endpoint in Micronaut

When learning any new framework, it always starts with building your first “Hello World” application. In Micronaut, this is incredibly simple, but also very powerful because you start using its key features like:

  • Controllers
  • Routing
  • Dependency Injection
  • Auto-generated boilerplate

In this post, you’ll learn how to create your first endpoint in Micronaut step-by-step using very simple language — even if you’re completely new to Micronaut or web development.

What is an Endpoint in Micronaut?

In simple words:

An endpoint is a URL path that your application listens to. When someone visits that path, your application returns a response.

Example:

  • You visit: http://localhost:8080/hello
  • You get response: Hello, World!

That’s what we are going to build.

Go through Installing Micronaut CLI and Setting Up Your First Project for setting up the micronaut env.

Step 1 — Create Your Micronaut Project

We will use Micronaut CLI to create the project:

Explanation:

  • com.kscodes.micronaut.helloworld: This is the package name.
  • --build=maven: We are using Maven.
  • --lang=java: We are using Java.

✅ After running this, Micronaut will automatically create a full project structure for you.

Step 2 — Understand Where to Write Code

Inside the generated project, navigate to:

This is where we will add our code.

You will already see a file Application.java which looks like this:

This is your application’s entry point — Micronaut automatically starts the application from here.

Step 3 — Create Your First Controller

A controller is where you define endpoints.

Create a new Java file:

HelloController.java

Code:

Explanation (in very simple words):

AnnotationMeaning
@Controller("/hello")This class handles requests that start with /hello.
@Get("/")This method handles GET requests sent to /hello/.
sayHello()The method returns the text "Hello, World!".

✅ So when someone visits http://localhost:8080/hello/, they will see:
Hello, World!

Step 4 — Run Your Application

Use the following command to start the server:

Micronaut will start your application and listen on port 8080 by default.

You should see something like:

Your First Endpoint in Micronaut

Step 5 — Test Your Endpoint

Open your browser or use a tool like Postman or curl.

URL:

Expected Output:

Hello, World!

🎉 Congratulations! You just built and ran your first Micronaut endpoint.

Step 6 — Customize the Endpoint

You can modify the controller to make it even more dynamic.

Updated Code:

Now if you visit:

You’ll see:

Hello, KSCodes!

✅ You have now built a dynamic endpoint using path variables.

Quick Recap

TaskWhat You Learned
Create ProjectUsing Micronaut CLI
ControllerHandling requests with annotations
EndpointReturn simple text responses
Dynamic InputUse of path variables

Conclusion

Micronaut makes it super easy to build REST APIs and microservices, even for absolute beginners. With just a few lines of code, you have created a fully functional web service that responds to HTTP requests.

In upcoming tutorials, you can explore:

  • Handling query parameters
  • Returning JSON responses
  • Adding database connections
  • Securing your endpoints
  • Testing your application

Reference : Micronaut Docs