0
0
Spring Bootframework~20 mins

DTO validation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.