Building a Complete CRUD Application with Micronaut

Building a CRUD (Create, Read, Update, Delete) application is a foundational skill for any backend developer. In this tutorial, we’ll use Micronaut and Maven to build a Customer Relationship Management (CRM) system. Micronaut’s fast startup time, minimal memory footprint, and annotation-based configuration make it an ideal choice for microservices and APIs.

We’ll walk through creating a complete application that allows us to manage customers in a PostgreSQL database. This guide is simple, beginner-friendly, and SEO-optimized to help you rank better and learn efficiently.

Micronaut CRUD Application

๐Ÿ“ฆ Project Setup

1. Directory Structure


src
 โ””โ”€โ”€ main
     โ”œโ”€โ”€ java
     โ”‚   โ””โ”€โ”€ com
     โ”‚       โ””โ”€โ”€ kscodes
     โ”‚           โ””โ”€โ”€ micronaut
     โ”‚               โ””โ”€โ”€ example
     โ”‚                   โ”œโ”€โ”€ controller
     โ”‚                   โ”œโ”€โ”€ model
     โ”‚                   โ”œโ”€โ”€ repository
     โ”‚                   โ””โ”€โ”€ service
     โ””โ”€โ”€ resources
         โ””โ”€โ”€ application.yml

2. pom.xml

Use the Micronaut Launch to generate a Maven project. Add dependencies:



    io.micronaut.data
    micronaut-data-jdbc


    io.micronaut.sql
    micronaut-jdbc-hikari


    io.micronaut
    micronaut-validation


    org.postgresql
    postgresql


๐Ÿง‘โ€๐Ÿ’ป Step-by-Step Implementation

3. Model: Customer.java


package com.kscodes.micronaut.example.model;

import io.micronaut.core.annotation.Introspected;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;

@Entity
@Introspected
public class Customer {

    @Id
    @GeneratedValue
    private Long id;

    @NotBlank
    private String name;

    private String email;

    // Getters and Setters
}

4. Repository: CustomerRepository.java


package com.kscodes.micronaut.example.repository;

import com.kscodes.micronaut.example.model.Customer;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface CustomerRepository extends CrudRepository {
}

5. Service: CustomerService.java


package com.kscodes.micronaut.example.service;

import com.kscodes.micronaut.example.model.Customer;
import com.kscodes.micronaut.example.repository.CustomerRepository;
import jakarta.inject.Singleton;

import java.util.Optional;

@Singleton
public class CustomerService {

    private final CustomerRepository repository;

    public CustomerService(CustomerRepository repository) {
        this.repository = repository;
    }

    public Iterable list() {
        return repository.findAll();
    }

    public Customer save(Customer customer) {
        return repository.save(customer);
    }

    public Optional find(Long id) {
        return repository.findById(id);
    }

    public void delete(Long id) {
        repository.deleteById(id);
    }
}

6. Controller: CustomerController.java


package com.kscodes.micronaut.example.controller;

import com.kscodes.micronaut.example.model.Customer;
import com.kscodes.micronaut.example.service.CustomerService;
import io.micronaut.http.annotation.*;

import java.util.Optional;

@Controller("/customers")
public class CustomerController {

    private final CustomerService service;

    public CustomerController(CustomerService service) {
        this.service = service;
    }

    @Get("/")
    public Iterable list() {
        return service.list();
    }

    @Post("/")
    public Customer save(@Body Customer customer) {
        return service.save(customer);
    }

    @Get("/{id}")
    public Optional get(Long id) {
        return service.find(id);
    }

    @Delete("/{id}")
    public void delete(Long id) {
        service.delete(id);
    }
}

โš™๏ธ application.yml Configuration


datasources:
  default:
    url: jdbc:postgresql://localhost:5432/customersdb
    username: postgres
    password: password
    driverClassName: org.postgresql.Driver
jpa:
  default:
    properties:
      hibernate:
        hbm2ddl:
          auto: update

๐Ÿงช Testing the Endpoints

Use Postman or curl to test your endpoints:


curl -X POST http://localhost:8080/customers \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'

๐Ÿ“Œ Conclusion

You just built a full-featured Micronaut CRUD application for managing customer data using Maven and PostgreSQL. We covered everything from setting up the project to creating models, repositories, services, and REST APIs.

This example gives you a solid base to extend into a full CRM application with features like filtering, pagination, authentication, etc.

๐Ÿ”— External References