KatInterview

Spring Boot REST API: Controller Design with DTOs and Validation

System DesignModel answer
MediumModel answerSystem Designspring-bootrest-apivalidation

Last updated

Build a Spring Boot controller for a banking resource with proper HTTP semantics, request validation, error handling, and tests.

You're building a customer account management API for a financial services platform using Spring Boot. Your task is to implement the web layer with proper REST conventions, validation, and testing.

Context

The system manages customer accounts. Each account has:

  • A unique identifier (generated)
  • A 10-digit account number (unique, cannot be changed after creation)
  • The account holder's name (required, 2–100 characters)
  • An account category: either CHECKING or SAVINGS
  • A current balance (non-negative decimal)
  • A status flag: ACTIVE, FROZEN, or CLOSED

What You Need to Build

1. Request and Response Models

Create separate data transfer objects for:

  • Creating a new account (all fields required except the ID)
  • Updating an existing account (only the holder name and status can be modified)
  • Returning account data to clients

Apply Jakarta Bean Validation annotations to enforce:

  • Account number must be exactly 10 numeric digits
  • Holder name must be 2–100 characters, not blank
  • Balance must be zero or positive
  • Immutable fields (account number, balance) should be explicitly rejected if present in update requests

2. Controller Endpoints

Implement AccountController at the base path /api/v1/accounts:

Create a new account:

  • HTTP method and path: POST /api/v1/accounts
  • Accepts a JSON payload with account details
  • Returns 201 Created with the created resource and a Location header pointing to the new resource
  • Returns 400 Bad Request for validation failures

Retrieve a single account:

  • HTTP method and path: GET /api/v1/accounts/{id}
  • Returns 200 OK with account details
  • Returns 404 Not Found if the account doesn't exist

List accounts with optional filtering:

  • HTTP method and path: GET /api/v1/accounts
  • Accepts an optional query parameter status (e.g., ?status=ACTIVE)
  • Returns 200 OK with an array of matching accounts

Update an account:

  • HTTP method and path: PUT /api/v1/accounts/{id}
  • Replaces the holder name and status with the provided values
  • Account number and balance cannot be changed through this endpoint
  • Returns 200 OK on success, 404 Not Found if the ID doesn't exist

3. Error Handling

Create a global exception handler using @RestControllerAdvice that catches validation errors and domain exceptions, returning a consistent error structure containing:

  • timestamp (when the error occurred)
  • status (HTTP status code as an integer)
  • error (the HTTP status reason phrase)
  • message (a human-readable error description)

4. Testing

Write a Spring MVC test slice using @WebMvcTest and MockMvc that verifies:

  • The POST endpoint rejects invalid input and returns 400
  • The POST endpoint accepts valid input, returns 201, and includes the Location header

Expectations

  • Use proper HTTP status codes and headers
  • Keep the controller thin—delegate business logic to a service layer
  • Return consistent error responses across all failure cases
  • Demonstrate that validation occurs before service methods are invoked

Hints

Hint 1

Separate your DTOs by operation—create, update, and response should be different types

Hint 2

Use @Valid in the controller and let Spring's validation handle constraint violations before your code runs

Hint 3

Build the Location header dynamically using ServletUriComponentsBuilder to handle proxies correctly