0
0
Spring Bootframework~10 mins

Why input validation is critical in Spring Boot - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add input validation annotation for a non-empty string in Spring Boot.

Spring Boot
@NotBlank(message = "Name cannot be empty")
private String [1];
Drag options to blanks, or click blank then click option'
Ausername
Bprice
Cage
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a numeric field like age or price with @NotBlank causes errors.
Leaving the field name blank or misspelled.
2fill in blank
medium

Complete the code to validate that an integer input is at least 18 in Spring Boot.

Spring Boot
@Min(value = [1], message = "Age must be at least 18")
private int age;
Drag options to blanks, or click blank then click option'
A16
B0
C18
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value less than 18 allows invalid ages.
Using a value greater than 18 unnecessarily restricts input.
3fill in blank
hard

Fix the error in the code to correctly validate an email input in Spring Boot.

Spring Boot
@Email(message = "Invalid email format")
private String [1];
Drag options to blanks, or click blank then click option'
AphoneNumber
Busername
Cpassword
DemailAddress
Attempts:
3 left
💡 Hint
Common Mistakes
Applying email validation to phone number or password fields.
Using a field name unrelated to email.
4fill in blank
hard

Fill both blanks to create a validated DTO class with a non-null name and age at least 21.

Spring Boot
public class UserDTO {
    @[1](message = "Name is required")
    private String name;

    @[2](value = 21, message = "Must be at least 21")
    private int age;
}
Drag options to blanks, or click blank then click option'
ANotNull
BMin
CNotBlank
DMax
Attempts:
3 left
💡 Hint
Common Mistakes
Using @NotNull instead of @NotBlank for strings allows empty names.
Using @Max instead of @Min for age validation.
5fill in blank
hard

Fill all three blanks to create a Spring Boot controller method that validates input and returns a response.

Spring Boot
@PostMapping("/register")
public ResponseEntity<String> registerUser(@Valid @RequestBody [1] user) {
    if (user.getAge() [2] 18) {
        return ResponseEntity.status(HttpStatus.[3]).body("User too young");
    }
    return ResponseEntity.ok("User registered");
}
Drag options to blanks, or click blank then click option'
AUserDTO
B<
CFORBIDDEN
DUserEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Using entity class instead of DTO for input validation.
Using wrong comparison operator like '>' instead of '<'.
Returning wrong HTTP status code.