⬅️ Back to Controller Index

Integration with Other Layers

The Integration responsibility describes how the Controller Layer connects to downstream layers in the application.

Layered Architecture Integration

Controller Layer (You are here)
       ↓
Service Layer (Business Logic)
       ↓
Repository Layer (Data Access)
       ↓
Database

Interaction Pattern

1. Controller Receives Request

@PostMapping
public ResponseEntity<SupplierDTO> create(@Valid @RequestBody CreateSupplierDTO dto) {
    // Request received, DTO validated

2. Controller Delegates to Service

The controller calls service methods but never implements business logic:

    // NO business logic here
    SupplierDTO created = supplierService.create(dto);  // Delegate

3. Service Performs Validation

The service layer validates business rules:

// In SupplierService
@Transactional
public SupplierDTO create(CreateSupplierDTO dto) {
    // Business validation
    supplierValidator.validateRequiredFields(dto);
    supplierValidator.validateUniquenessOnCreate(dto.getName());
    
    // Convert DTO β†’ Entity
    Supplier entity = mapper.toEntity(dto);

4. Service Calls Repository

The service persists data via repository methods:

    // Persist to database
    Supplier saved = repository.save(entity);
    
    // Return DTO to controller
    return mapper.toDTO(saved);
}

5. Controller Builds HTTP Response

The controller receives the result and wraps it in a response:

    // Build HTTP response
    URI location = ServletUriComponentsBuilder.fromCurrentRequest()
        .path("/{id}")
        .buildAndExpand(created.getId())
        .toUri();
    
    return ResponseEntity.created(location).body(created);  // 201 CREATED
}

Data Flow Diagram

Client (JSON)
    ↓
Controller
    β”œβ”€ Receives HTTP request
    β”œβ”€ Validates with @Valid
    β”œβ”€ Checks authorization with @PreAuthorize
    └─ Delegates to service
    ↓
Service Layer
    β”œβ”€ Validates business rules
    β”œβ”€ Transforms DTO β†’ Entity
    β”œβ”€ Calls repository
    └─ Transforms Entity β†’ DTO
    ↓
Repository Layer
    β”œβ”€ Executes SQL/JPQL queries
    └─ Returns entities
    ↓
Database
    └─ Persists/retrieves data
    ↑
Entity is returned through layers
    ↓
Controller (DTO)
    └─ Returns HTTP response (JSON)

Responsibility Boundaries

Controller DOES: - βœ… Handle HTTP concerns (routing, status codes) - βœ… Validate input with @Valid - βœ… Check authorization with @PreAuthorize - βœ… Convert DTO β†”οΈŽ JSON - βœ… Call service methods

Controller DOES NOT: - ❌ Implement business logic - ❌ Access repositories directly - ❌ Connect to databases - ❌ Validate business rules - ❌ Manage transactions

Service DOES: - βœ… Implement business logic - βœ… Validate business rules - βœ… Manage transactions - βœ… Call repositories - βœ… Handle exceptions

Repository DOES: - βœ… Execute database queries - βœ… Manage entities - βœ… Handle pagination/sorting - βœ… Translate to domain exceptions


⬅️ Back to Controller Index