What if your app could catch bad data before it even reaches your code?
Why DTO validation in Spring Boot? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web app where users submit forms with their data. You manually check every field in your code to see if it's correct, like making sure emails look right or numbers are positive.
Manually checking each input is slow and easy to forget. If you miss a check, bad data sneaks in, causing bugs or crashes. It's like proofreading a long letter without spellcheck--tiring and error-prone.
DTO validation lets you declare rules for your data in one place. The framework automatically checks inputs before your app uses them, catching errors early and keeping your code clean and safe.
if(email == null || !email.contains("@")) { throw new Exception("Invalid email"); }
@Email private String email;
It makes your app trust user data confidently and reduces bugs by automating input checks.
When a user signs up, DTO validation ensures their email and password meet rules before saving, preventing invalid accounts.
Manual input checks are slow and risky.
DTO validation automates and centralizes data rules.
This keeps apps safer and code simpler.
Practice
Solution
Step 1: Understand DTO role
A DTO (Data Transfer Object) carries data between processes and needs validation to ensure data is correct.Step 2: Purpose of validation
Validation checks input data early to prevent bad data from reaching business logic or database.Final Answer:
To check and ensure input data meets rules before processing -> Option DQuick Check:
DTO validation = input data check [OK]
- Confusing validation with database optimization
- Thinking validation generates UI
- Mixing validation with authentication
Solution
Step 1: Understand annotations meaning
@NotNull only checks for null, but allows empty strings. @NotEmpty checks for both null and empty strings.Step 2: Choose correct annotation
To ensure a field is neither null nor empty, @NotEmpty is the best choice.Final Answer:
@NotEmpty -> Option BQuick Check:
@NotEmpty = no null or empty [OK]
- Using @NotNull but allowing empty strings
- Confusing @Valid with field validation
- Using @Size without min value
public class UserDTO {
@NotNull
private String username;
@Min(18)
private int age;
// getters and setters
}What happens if a request sends
username=null and age=16 when validated with @Valid?Solution
Step 1: Check username validation
@NotNull on username means null value is invalid, so username=null fails validation.Step 2: Check age validation
@Min(18) means age must be at least 18. Given age=16, this fails validation.Final Answer:
Validation fails for both username and age fields -> Option AQuick Check:
@NotNull + @Min(18) fail for null and age 16 [OK]
- Assuming int fields can't fail validation
- Ignoring @NotNull effect on String
- Thinking validation passes if one field is valid
@PostMapping("/users")
public ResponseEntity<String> addUser(UserDTO user) {
// save user
return ResponseEntity.ok("User added");
}Solution
Step 1: Check parameter annotations
To validate JSON input as DTO, @RequestBody is needed to bind request body to UserDTO.Step 2: Check validation annotation
@Valid is also needed to trigger validation, but missing @RequestBody causes binding failure first.Final Answer:
Missing @RequestBody annotation on UserDTO parameter -> Option CQuick Check:
@RequestBody needed for JSON binding [OK]
- Forgetting @RequestBody causes no binding
- Thinking @Valid alone binds JSON
- Assuming return type must be void
Solution
Step 1: Understand nested validation
Spring Boot requires @Valid on nested DTO fields to trigger validation of inner objects.Step 2: Why @Valid on nested field
@NotNull only checks presence, but @Valid triggers validation of nested object's fields.Final Answer:
Add @Valid annotation on the nested DTO field inside the parent DTO -> Option AQuick Check:
@Valid on nested field triggers inner validation [OK]
- Using only @NotNull on nested DTO
- Assuming parent @Valid covers nested fields
- Skipping @Valid and expecting automatic nested validation
