0
0
Spring Bootframework~10 mins

Request validation preview in Spring Boot - Interactive Code Practice

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

Complete the code to add a validation annotation that ensures the 'name' field is not empty.

Spring Boot
public class UserRequest {
    @[1]
    private String name;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
ANotEmpty
BSize
CPattern
DEmail
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Size without specifying min value
Using @Email on a non-email field
2fill in blank
medium

Complete the code to validate that the 'age' field is at least 18.

Spring Boot
public class UserRequest {
    @Min([1])
    private int age;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
A18
B0
C21
D16
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value less than 18
Confusing @Min with @Max
3fill in blank
hard

Fix the error in the validation annotation to ensure the 'email' field is a valid email address.

Spring Boot
public class UserRequest {
    @[1]
    private String email;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
ANotBlank
BSize(min = 5)
CPattern(regexp = ".*@.*")
DEmail
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Pattern with incorrect regex
Using @NotBlank which only checks for non-empty
4fill in blank
hard

Fill both blanks to validate that the 'password' field is not null and has a minimum length of 8.

Spring Boot
public class UserRequest {
    @[1]
    @[2](min = 8)
    private String password;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
ANotNull
BSize
CNotEmpty
DMin
Attempts:
3 left
💡 Hint
Common Mistakes
Using @NotEmpty instead of @NotNull
Using @Min instead of @Size for string length
5fill in blank
hard

Fill all three blanks to create a map of field names to their validation error messages using Spring's BindingResult.

Spring Boot
Map<String, String> errors = new HashMap<>();
for (FieldError error : bindingResult.getFieldErrors()) {
    errors.put(error.get[1](), error.get[2]());
}
return ResponseEntity.badRequest().body(errors.get([3]));
Drag options to blanks, or click blank then click option'
AField
BDefaultMessage
C"email"
DCode
Attempts:
3 left
💡 Hint
Common Mistakes
Using error.getCode() instead of getDefaultMessage()
Using wrong key to access the map