Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a DTO in Spring Boot?
DTO stands for Data Transfer Object. It is a simple object used to carry data between processes, often between client and server, without any business logic.
Click to reveal answer
beginner
Why do we validate DTOs in Spring Boot?
We validate DTOs to ensure the data received from users or clients is correct and safe before processing it. This helps prevent errors and security issues.
Click to reveal answer
beginner
Which annotation is used to trigger validation on a DTO in a Spring Boot controller?
The @Valid annotation is used on a DTO parameter in a controller method to trigger validation based on the constraints defined in the DTO class.
Click to reveal answer
intermediate
Name three common validation annotations used in Spring Boot DTOs.
Common annotations include @NotNull (value must not be null), @Size (limits string or collection size), and @Email (checks for valid email format).
Click to reveal answer
intermediate
How does Spring Boot handle validation errors in a REST API?
Spring Boot automatically returns a 400 Bad Request response with details about which fields failed validation when a DTO annotated with @Valid fails validation.
Click to reveal answer
Which annotation do you use to validate a DTO parameter in a Spring Boot controller?
A@Valid
B@Autowired
C@RequestMapping
D@Service
✗ Incorrect
The @Valid annotation triggers validation on the DTO based on its constraints.
What does the @NotNull annotation ensure in a DTO field?
AField must not be null
BField must be a number
CField must be unique
DField must be a valid email
✗ Incorrect
@NotNull ensures the field is not null when validated.
If a DTO validation fails in Spring Boot, what HTTP status code is typically returned?
A200 OK
B400 Bad Request
C404 Not Found
D500 Internal Server Error
✗ Incorrect
Validation failures return 400 Bad Request with error details.
Which annotation checks that a string field contains a valid email format?
A@Pattern
B@Size
C@Email
D@NotBlank
✗ Incorrect
@Email validates that the string is a valid email address.
Where do you place validation annotations like @NotNull or @Size in a Spring Boot project?
AOn repository interfaces
BOn controller methods
COn service classes
DOn DTO class fields
✗ Incorrect
Validation annotations are placed on DTO fields to define rules for input data.
Explain how DTO validation works in a Spring Boot REST controller from receiving data to handling errors.
Think about the flow from client input to server response.
You got /4 concepts.
List and describe three common validation annotations used in Spring Boot DTOs and what they check.
Focus on annotations that check presence, size, and format.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using DTO validation in a Spring Boot application?
easy
A. To handle user authentication and login
B. To speed up database queries automatically
C. To generate HTML pages from data
D. To check and ensure input data meets rules before processing
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 D
Quick Check:
DTO validation = input data check [OK]
Hint: Validation means checking input data early [OK]
Common Mistakes:
Confusing validation with database optimization
Thinking validation generates UI
Mixing validation with authentication
2. Which annotation is used on a DTO field to require that it must not be empty or null?
easy
A. @Size(min = 1)
B. @NotEmpty
C. @NotNull
D. @Valid
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 B
Quick Check:
@NotEmpty = no null or empty [OK]
Hint: Use @NotEmpty to block null and empty strings [OK]
Common Mistakes:
Using @NotNull but allowing empty strings
Confusing @Valid with field validation
Using @Size without min value
3. Given this DTO class snippet:
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?
medium
A. Validation fails for both username and age fields
B. Validation passes because age is int and can't be null
C. Validation fails only for age field
D. Validation passes because @NotNull is ignored on String
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 A
Quick Check:
@NotNull + @Min(18) fail for null and age 16 [OK]
Hint: Check each annotation rule against input values [OK]
Common Mistakes:
Assuming int fields can't fail validation
Ignoring @NotNull effect on String
Thinking validation passes if one field is valid
4. Identify the error in this controller method for validating a DTO:
@PostMapping("/users")
public ResponseEntity<String> addUser(UserDTO user) {
// save user
return ResponseEntity.ok("User added");
}
medium
A. Missing @Validated annotation on controller class
B. Method should return void instead of ResponseEntity
C. Missing @RequestBody annotation on UserDTO parameter
D. No error, code is correct
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 C
Quick Check:
@RequestBody needed for JSON binding [OK]
Hint: Use @RequestBody to bind JSON to DTO [OK]
Common Mistakes:
Forgetting @RequestBody causes no binding
Thinking @Valid alone binds JSON
Assuming return type must be void
5. You want to validate a DTO with a nested object, where the nested object also needs validation. Which is the correct way to enable validation on the nested DTO field?
hard
A. Add @Valid annotation on the nested DTO field inside the parent DTO
B. Add @NotNull on the nested DTO field only
C. Add @Valid on the parent DTO class only
D. No annotation needed, nested DTOs are validated automatically
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 A
Quick Check:
@Valid on nested field triggers inner validation [OK]
Hint: Use @Valid on nested DTO fields for full validation [OK]
Common Mistakes:
Using only @NotNull on nested DTO
Assuming parent @Valid covers nested fields
Skipping @Valid and expecting automatic nested validation