Bird
Raised Fist0
Spring Bootframework~20 mins

DTO validation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
DTO Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a DTO field annotated with @NotNull is null?
Consider a Spring Boot REST controller receiving a DTO with a field annotated as @NotNull. What is the expected behavior if the client sends a null value for that field?
Spring Boot
public class UserDTO {
    @NotNull
    private String username;

    // getters and setters
}

@PostMapping("/users")
public ResponseEntity<String> createUser(@Valid @RequestBody UserDTO userDTO) {
    return ResponseEntity.ok("User created");
}
AThe controller method executes normally and creates the user with a null username.
BThe request is rejected with a 400 Bad Request and a validation error message.
CThe application throws a NullPointerException at runtime.
DThe server responds with 500 Internal Server Error due to validation failure.
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles validation annotations with @Valid.
📝 Syntax
intermediate
2:00remaining
Which DTO field annotation correctly validates a string length between 5 and 10?
You want to ensure a DTO string field has a length of at least 5 and at most 10 characters. Which annotation is correct?
Spring Boot
public class ProductDTO {
    // field here
    @Size(min = 5, max = 10)
    private String code;

    // getters and setters
}
A
@Size(min = 5)
private String code;
B
@Length(min = 5, max = 10)
private String code;
C
@Range(min = 5, max = 10)
private String code;
D
@Size(min = 5, max = 10)
private String code;
Attempts:
2 left
💡 Hint
Look for the standard javax.validation annotation for string length.
🔧 Debug
advanced
2:00remaining
Why does this DTO validation not trigger an error for an empty string?
Given this DTO field annotated with @NotNull, why does sending an empty string not cause a validation error?
Spring Boot
public class LoginDTO {
    @NotNull
    private String password;

    // getters and setters
}
A@NotNull only checks for null, not empty strings, so empty string passes validation.
BEmpty strings are treated as null by default, so validation should fail but does not due to a bug.
C@NotNull also checks for empty strings, so this behavior is unexpected and indicates misconfiguration.
DThe validation is skipped because the field is private and lacks a public setter.
Attempts:
2 left
💡 Hint
Consider what @NotNull actually validates.
state_output
advanced
2:00remaining
What is the validation error message when a @Min(18) annotated field is 16?
A DTO has an integer field annotated with @Min(18). If the client sends 16, what validation error message is generated by default?
Spring Boot
public class AgeDTO {
    @Min(18)
    private int age;

    // getters and setters
}
Amust be greater than or equal to 18
Bvalue 16 is invalid
Cage must be at least 18 years old
Dminimum value violation
Attempts:
2 left
💡 Hint
Look at the default message for @Min in javax.validation.
🧠 Conceptual
expert
3:00remaining
Why use a separate DTO class with validation instead of validating entity classes directly?
In Spring Boot applications, why is it recommended to create separate DTO classes with validation annotations instead of applying validation directly on JPA entity classes?
AValidation on entities causes database schema errors, so DTOs are safer.
BEntity classes cannot have annotations, so validation must be on DTOs.
CDTOs separate concerns, avoid exposing internal entity structure, and allow tailored validation rules per use case.
DDTOs automatically validate without needing @Valid, unlike entities.
Attempts:
2 left
💡 Hint
Think about design principles and security.

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

  1. Step 1: Understand DTO role

    A DTO (Data Transfer Object) carries data between processes and needs validation to ensure data is correct.
  2. Step 2: Purpose of validation

    Validation checks input data early to prevent bad data from reaching business logic or database.
  3. Final Answer:

    To check and ensure input data meets rules before processing -> Option D
  4. 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

  1. Step 1: Understand annotations meaning

    @NotNull only checks for null, but allows empty strings. @NotEmpty checks for both null and empty strings.
  2. Step 2: Choose correct annotation

    To ensure a field is neither null nor empty, @NotEmpty is the best choice.
  3. Final Answer:

    @NotEmpty -> Option B
  4. 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

  1. Step 1: Check username validation

    @NotNull on username means null value is invalid, so username=null fails validation.
  2. Step 2: Check age validation

    @Min(18) means age must be at least 18. Given age=16, this fails validation.
  3. Final Answer:

    Validation fails for both username and age fields -> Option A
  4. 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

  1. Step 1: Check parameter annotations

    To validate JSON input as DTO, @RequestBody is needed to bind request body to UserDTO.
  2. Step 2: Check validation annotation

    @Valid is also needed to trigger validation, but missing @RequestBody causes binding failure first.
  3. Final Answer:

    Missing @RequestBody annotation on UserDTO parameter -> Option C
  4. 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

  1. Step 1: Understand nested validation

    Spring Boot requires @Valid on nested DTO fields to trigger validation of inner objects.
  2. Step 2: Why @Valid on nested field

    @NotNull only checks presence, but @Valid triggers validation of nested object's fields.
  3. Final Answer:

    Add @Valid annotation on the nested DTO field inside the parent DTO -> Option A
  4. 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