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.

๐ฆ Project Setup
1. Directory Structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<dependency> <groupId>io.micronaut.data</groupId> <artifactId>micronaut-data-jdbc</artifactId> </dependency> <dependency> <groupId>io.micronaut.sql</groupId> <artifactId>micronaut-jdbc-hikari</artifactId> </dependency> <dependency> <groupId>io.micronaut</groupId> <artifactId>micronaut-validation</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> |
๐งโ๐ป Step-by-Step Implementation
3. Model: Customer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
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<Customer, Long> { } |
5. Service: CustomerService.java
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 |
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<Customer> list() { return repository.findAll(); } public Customer save(Customer customer) { return repository.save(customer); } public Optional<Customer> find(Long id) { return repository.findById(id); } public void delete(Long id) { repository.deleteById(id); } } |
6. Controller: CustomerController.java
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 |
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<Customer> list() { return service.list(); } @Post("/") public Customer save(@Body Customer customer) { return service.save(customer); } @Get("/{id}") public Optional<Customer> get(Long id) { return service.find(id); } @Delete("/{id}") public void delete(Long id) { service.delete(id); } } |
โ๏ธ application.yml
Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 4 5 6 |
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.