Skip to main content

MODULE 3 - Layered Architecture and Complete CRUD

Module Objectives​

This module will enable you to:

  • Master the 3-layer architecture (Controller, Service, Repository)
  • Understand the role and importance of the Service layer
  • Implement data validation with Bean Validation
  • Professionally handle HTTP errors with ResponseEntity
  • Create a complete and robust REST API

Module Content​

Theory​

Chapter 1: The Service Layer in Detail​

  • Role and responsibilities of the Service layer
  • Separation of concerns
  • Business logic vs data access
  • Dependency injection
  • Complete architecture Controller → Service → Repository

Chapter 2: Data Validation​

  • Introduction to Bean Validation (JSR 380)
  • Validation annotations:
    • @NotNull, @NotEmpty, @NotBlank
    • @Size, @Min, @Max
    • @Email, @Pattern
    • @Past, @Future
  • @Valid in controllers
  • Custom error messages
  • Validation groups
  • Custom validation

Chapter 3: HTTP Error Handling​

  • Understanding ResponseEntity
  • HTTP status codes (200, 201, 204, 400, 404, 500)
  • Exception handling
  • @ExceptionHandler
  • @ControllerAdvice for global management
  • Creating custom error responses
  • Best practices

Chapter 4: Professional 3-Layer Architecture​

  • Overview of the architecture
  • Complete data flow
  • Naming conventions
  • Package organization
  • DTOs vs Entities (introduction)
  • Professional best practices

Practical Work​

TP3.1: Complete CRUD Architecture​

Objective: Create a complete REST API with validation and error handling

Content:

  • 3-layer architecture (Controller, Service, Repository)
  • Product entity with validation
  • Complete CRUD operations
  • Input data validation
  • Professional error handling (404, 400, 500)
  • ResponseEntity for all responses
  • Testing with Postman

Estimated duration: 4-6 hours

Prerequisites:

  • Module 2 completed (TP2.1 and TP2.2)
  • All chapters of Module 3 read

Level: Intermediate


TP3.2: Standalone CRUD (Challenge)​

Objective: Same statement as TP3.1 but without any provided code — to be done independently

Content:

  • Same student management application
  • Clear instructions: what to do, which endpoints, which HTTP codes
  • No technical details, no code provided
  • 5 bonus exercises (search, stats, pagination, errors, PostgreSQL)
  • Review questions and evaluation criteria

Estimated duration: 2-3 hours

Prerequisites: Understanding of Module 3 concepts

Level: Intermediate-Advanced (autonomy required)


Prerequisites​

Required Knowledge​

Before starting this module, you should master:

✅ Creating Spring Boot projects with Spring Initializr
✅ Structure of a Spring Boot project
✅ Configuration (application.properties / application.yml)
✅ Basic annotations (@RestController, @Service, @Repository)
✅ HTTP mapping annotations (@GetMapping, @PostMapping, etc.)
✅ JPA and repositories (findById, findAll, save, deleteById)
✅ Simple derived methods (findBy, And, Or)
✅ H2 in-memory and persistent mode

If you are not comfortable with these concepts, return to Module 2.


Estimated Duration​

Theory: 8-10 hours (4 chapters)
Practice: 4-6 hours (1 complete TP)
Total: 12-16 hours (2-3 sessions of 4-6 hours)


Additional Resources​

Official Documentation​


Success Criteria​

By the end of this module, you should be able to:

  • ✅ Create a professional 3-layer architecture
  • ✅ Implement data validation with Bean Validation
  • ✅ Handle HTTP errors with ResponseEntity
  • ✅ Use the correct HTTP status codes
  • ✅ Implement all CRUD operations
  • ✅ Properly separate business logic from data access
  • ✅ Create a robust and professional REST API

Next Steps​

Once this module is completed, you will be ready for:

  • Module 4: Databases in Production (PostgreSQL, Jakarta/Javax, Hibernate, Lombok)
  • Module 5: Spring Core - Advanced IoC and DI
  • Module 6: Advanced Spring Data JPA (Joins, JPQL, Optimization)

Why This Module Is Important​

Before Module 3​

❌ Fragile code (no validation)
❌ Basic error management
❌ Business logic in the controller
❌ Difficult to test
❌ Non-professional

After Module 3​

✅ Robust code (complete validation)
✅ Professional error management
✅ Clear separation of responsibilities
✅ Easily testable
✅ Production-ready

Start with Chapter 1