Concept Flow - DTO validation
Receive HTTP Request
Bind data to DTO object
Validate DTO fields
Process request
Send response
The flow shows how Spring Boot receives data, binds it to a DTO, validates it, and either processes or returns errors.
import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; public record UserDTO( @NotBlank String name, @Email String email ) {} @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody UserDTO user) { return ResponseEntity.ok("User created"); }
| Step | Action | DTO Field | Validation Check | Result | Next Step |
|---|---|---|---|---|---|
| 1 | Receive HTTP POST /users | - | - | - | Bind data to UserDTO |
| 2 | Bind JSON to UserDTO | name | Check @NotBlank | Pass if not empty | Validate next field |
| 3 | Bind JSON to UserDTO | Check @Email format | Pass if valid email | Validation complete | |
| 4 | Validation result | - | - | All fields valid | Call createUser method |
| 5 | Process request | - | - | Return 200 OK with message | Send response |
| 6 | If validation fails | - | - | Return 400 Bad Request with errors | Send error response |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| user.name | null | "Alice" | "Alice" | "Alice" |
| user.email | null | null | "alice@example.com" | "alice@example.com" |
| validationErrors | empty | empty | empty | empty or error list |
DTO validation in Spring Boot: - Use @Valid on controller method parameter - Annotate DTO fields with validation annotations like @NotBlank, @Email - Spring binds JSON to DTO, then validates automatically - If validation fails, returns 400 error with messages - If valid, controller method executes normally