Request validation checks the received contract before domain work. Persistence constraints and domain invariants still need their own enforcement.
Before you start
You should know Java classes, dependency injection and basic HTTP requests. Identify where a call crosses a framework-managed boundary. The snippets illustrate a focused mechanism; database configuration, application wiring and authentication must be supplied by the surrounding application when applicable.
The practical goal is to reason through this situation: Reject a missing quantity before calling the order service. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Validate the received shape
Check required fields and scalar constraints before invoking domain services.
Step 2: Enforce domain meaning
Existence, inventory and allowed quantities require business checks beyond DTO syntax.
Step 3: Authorize the operation
Verify the caller may act on the referenced resource independently of data validity.
Worked scenario
Reject a missing quantity before calling the order service.
An order DTO with a positive quantity may be structurally valid while referencing another customer’s cart. A field validator cannot establish ownership. Keep shape failures, domain conflicts and authorization failures distinct so clients receive useful outcomes without learning sensitive internal details.
Common mistake
A valid DTO can still refer to an unauthorized or nonexistent resource.
Verify the behavior
Test missing quantity, unavailable stock, nonexistent references and unauthorized ownership separately.
Interview exercise
Validate a create request fully.
Answer and reasoning
Apply shape constraints, domain checks and authorization separately, returning stable error details without exposing internals.
Continue learning
Compare the scenario with the Spring Boot interview questions and test your understanding with the Spring Boot MCQs. For terminology and implementation details, consult the reference material.