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:


{
  "id": 1,
  "name": "John Doe",
  "grade": "5th"
}

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:


mn create-app com.kscodes.micronaut.jsonexample --build=maven --lang=java

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)


package com.kscodes.micronaut.jsonexample;

public class Student {
    private Long id;
    private String name;
    private String grade;

    public Student() {
    }

    public Student(Long id, String name, String grade) {
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getGrade() {
        return grade;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

✅ This is a simple POJO that holds student data.

Step 2: Create a REST Controller


package com.kscodes.micronaut.jsonexample;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/students")
public class StudentController {

    @Get("/sample")
    public Student getSampleStudent() {
        return new Student(1L, "John Doe", "5th");
    }
}

✅ 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:


{
  "id": 1,
  "name": "John Doe",
  "grade": "5th"
}

🎉 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:


package com.kscodes.micronaut.jsonexample;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import java.util.List;
@Controller("/students")
public class StudentController {
    @Get("/")
    public List getAllStudents() {
        return List.of(
                new Student(1L, "John Doe", "5th"),
                new Student(2L, "Jane Smith", "6th")
        );
    }
}

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

You will get:


[
  {
    "id": 1,
    "name": "John Doe",
    "grade": "5th"
  },
  {
    "id": 2,
    "name": "Jane Smith",
    "grade": "6th"
  }
]

Accepting JSON in POST Requests

You can also receive JSON from clients.

Create POST Endpoint


package com.kscodes.micronaut.jsonexample;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Post;

@Controller("/students")
public class StudentController {

    @Post("/")
    public Student createStudent(@Body Student student) {
        // In real app, you would save to DB
        return student;
    }
}

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

Test with curl


curl -X POST http://localhost:8080/students/ \
   -H "Content-Type: application/json" \
   -d '{"id":3, "name":"Sam Wilson", "grade":"7th"}'

Response:


{
  "id": 3,
  "name": "Sam Wilson",
  "grade": "7th"
}

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.