Micronaut makes it very easy to create REST APIs using simple annotations. In this tutorial, we will explore how to use annotations like @Controller and @Get/@Post in Micronaut to build REST endpoints for a School Management System.

Project Overview
We will build a simple School API that allows us to:
- Add a student
- View student details
- List all students
Package structure: com.kscodes.school
Prerequisites
- Java 21 installed
- Micronaut CLI installed
- Maven
- VS Code or any IDE
Step 1: Create a Micronaut Project
Use Micronaut CLI to generate the project:
1 2 3 |
mn create-app com.kscodes.school --java-version 21 --build maven |
Or use Micronaut Launch and select Maven.
Open the project in VS Code.
Step 2: Create the Student Domain Class
Create Student.java
in: src/main/java/com/kscodes/school/model/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kscodes.school.model; import java.util.UUID; public class Student { private UUID id; private String name; private String grade; private int age; public Student(String name, String grade, int age) { this.id = UUID.randomUUID(); this.name = name; this.grade = grade; this.age = age; } public UUID getId() { return id; } public String getName() { return name; } public String getGrade() { return grade; } public int getAge() { return age; } } |
Step 3: Create a Student Service
Create StudentService.java
in: src/main/java/com/kscodes/school/service/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kscodes.school.service; import com.kscodes.school.model.Student; import jakarta.inject.Singleton; import java.util.*; @Singleton public class StudentService { private final Map<UUID, Student> students = new HashMap<>(); public Student addStudent(String name, String grade, int age) { Student student = new Student(name, grade, age); students.put(student.getId(), student); return student; } public Optional<Student> getStudent(UUID id) { return Optional.ofNullable(students.get(id)); } public Collection<Student> getAllStudents() { return students.values(); } } |
Step 4: Create REST Controller using @Controller, @Get and @Post
Create StudentController.java
in: src/main/java/com/kscodes/school/controller/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kscodes.school.controller; import com.kscodes.school.model.Student; import com.kscodes.school.service.StudentService; import io.micronaut.http.HttpResponse; import io.micronaut.http.annotation.*; import jakarta.inject.Inject; import java.util.*; import java.util.UUID; @Controller("/students") public class StudentController { @Inject StudentService studentService; // Add Student (POST) @Post("/") public Student addStudent(@Body Map<String, Object> request) { String name = (String) request.get("name"); String grade = (String) request.get("grade"); int age = Integer.parseInt(request.get("age").toString()); return studentService.addStudent(name, grade, age); } // Get Student by ID (GET) @Get("/{id}") public HttpResponse<Student> getStudent(UUID id) { return studentService.getStudent(id) .map(HttpResponse::ok) .orElse(HttpResponse.notFound()); } // List All Students (GET) @Get("/") public Collection<Student> getAllStudents() { return studentService.getAllStudents(); } } |
Step 5: Run and Test the API
Build and run the application:
1 2 3 |
mvnw mn:run |
Add a Student
1 2 3 |
curl -X POST http://localhost:8080/students -H "Content-Type: application/json" -d '{"name":"Alice","grade":"5th","age":10}' |
Get Student by ID
1 2 3 |
curl http://localhost:8080/students/{studentId} |
List All Students
1 2 3 |
curl http://localhost:8080/students |
Summary
- Used
@Controller
to define REST endpoints - Used
@Post
for creating data - Used
@Get
for fetching data - Built a simple School Management REST API
This simple example shows how @Controller and @Get/@Post in Micronaut makes it easy to build REST APIs using annotations. In future posts, we will add persistence using databases, input validations, and advanced features!
You can also go through Building REST API in Micronaut (Banking App Example) for some more practice.