Creating REST Endpoints with @Controller and @Get/@Post in Micronaut (School Management Example)

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.

REST Endpoints with @Controller and @Get/@Post in Micronaut

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:


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/


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/


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 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 getStudent(UUID id) {
        return Optional.ofNullable(students.get(id));
    }

    public Collection 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/


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 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 getStudent(UUID id) {
        return studentService.getStudent(id)
                .map(HttpResponse::ok)
                .orElse(HttpResponse.notFound());
    }

    // List All Students (GET)
    @Get("/")
    public Collection getAllStudents() {
        return studentService.getAllStudents();
    }
}

Step 5: Run and Test the API

Build and run the application:


mvnw mn:run

Add a Student


curl -X POST http://localhost:8080/students -H "Content-Type: application/json" -d '{"name":"Alice","grade":"5th","age":10}'

Get Student by ID


curl http://localhost:8080/students/{studentId}

List All Students


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.