Return JSON from REST Endpoints in Micronaut

When you build REST APIs, one of the most common tasks is to send data in JSON format.
JSON (JavaScript Object Notation) is:

  • Easy for machines to parse.
  • Easy for humans to read.
  • The most common format used in web APIs.

It is super easy to return JSON from REST endpoints in Micronaut. In this post, we’ll walk through:

  • Why we return JSON.
  • How Micronaut handles JSON.
  • Complete code examples.
  • How Micronaut automatically converts Java objects to JSON.
Return JSON from REST Endpoints in Micronaut

Why Return JSON?

Let’s say you are building a School Management System.
When a client (browser, mobile app, etc.) requests data, they expect to receive:

You don’t want to manually convert Java objects to JSON strings.
Micronaut does that for you automatically!

Micronaut and JSON — The Magic of Jackson

Micronaut uses Jackson (a popular Java library) to convert Java objects to JSON and back.

  • When you return a Java object from a controller, Micronaut automatically converts it into JSON.
  • When you receive JSON in a POST request, Micronaut can automatically convert it into a Java object.

This is called Serialization (Java → JSON) and Deserialization (JSON → Java).

Setting Up the Project

We will use Maven for this tutorial.

Generate Project (using Micronaut Launch or CLI)

If you are using CLI:

Creating a Simple JSON Endpoint

Let’s say we want to return details of a student.

Step 1: Create a Student POJO (Plain Old Java Object)

✅ This is a simple POJO that holds student data.

Step 2: Create a REST Controller

✅ Notice:

  • We simply return a Student object.
  • Micronaut will automatically convert it into JSON!

Testing the Endpoint

Run your Micronaut application:

mvn mn:run

Now open your browser or use curl:

http://localhost:8080/students/sample

OR

curl http://localhost:8080/students/sample

You will get:

🎉 Success! — Micronaut automatically converted your Java object into JSON.

Returning Lists of JSON Objects

What if you want to return multiple students?

Simply return a list:

👉 When you hit:
http://localhost:8080/students/

You will get:

Accepting JSON in POST Requests

You can also receive JSON from clients.

Create POST Endpoint

✅ The @Body annotation tells Micronaut to map the JSON request body into a Student object.

Test with curl

Response:

Summary Table

TaskMicronaut Feature
Return JSONReturn Java objects
Receive JSONUse @Body
SerializationAutomatic with Jackson
No extra codeMicronaut does the work

Conclusion

  • ✅ Super easy to return JSON from REST endpoints in Micronaut.
  • ✅ You don’t need to manually handle JSON conversion.
  • ✅ Micronaut + Jackson handle serialization/deserialization automatically.

You just return Java objects, and Micronaut handles the rest.

Further Reading and References

If you’d like to explore more about returning JSON with Micronaut and related concepts, check out these helpful resources:

  1. Micronaut Official Documentation — JSON Binding
    👉 https://docs.micronaut.io/latest/guide/#jsonBinding
    This section explains how Micronaut automatically handles JSON serialization and deserialization using Jackson.
  2. Jackson Project (used by Micronaut for JSON)
    👉 https://github.com/FasterXML/jackson
    Learn more about the library that powers JSON processing behind the scenes in Micronaut.
  3. REST API Tutorial — What is JSON?
    👉 https://restfulapi.net/json-introduction/
    For complete beginners who want to better understand JSON format.