0
0
Spring Bootframework~20 mins

@Email and @Pattern in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Validation Mastery with @Email and @Pattern
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when @Email validation fails?
Consider a Spring Boot REST controller receiving a user email annotated with @Email. What is the typical behavior when the email format is invalid?
Spring Boot
public class User {
    @Email
    private String email;

    // getters and setters
}

@PostMapping("/register")
public ResponseEntity<String> registerUser(@Valid @RequestBody User user) {
    return ResponseEntity.ok("User registered");
}
AA validation error is thrown and the request returns a 400 Bad Request response automatically.
BThe controller method executes normally and saves the user with the invalid email.
CThe application crashes with a NullPointerException.
DThe invalid email is automatically corrected to a valid format.
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles validation annotations with @Valid.
📝 Syntax
intermediate
2:00remaining
Which @Pattern regex correctly validates a 5-digit US ZIP code?
You want to validate a US ZIP code that must be exactly 5 digits using @Pattern. Which regex pattern is correct?
Spring Boot
public class Address {
    @Pattern(regexp = "???", message = "Invalid ZIP code")
    private String zipCode;

    // getters and setters
}
A"[a-zA-Z]{5}"
B"\d{5,}"
C"\\d{5}"
D"[0-9]{4}"
Attempts:
2 left
💡 Hint
Remember to escape backslashes in Java strings.
🔧 Debug
advanced
2:00remaining
Why does this @Email validation not work as expected?
Given this code snippet, why does the @Email annotation not trigger validation errors for invalid emails?
Spring Boot
public class Contact {
    @Email
    private String email;

    // getters and setters
}

@PostMapping("/contact")
public String submitContact(@RequestBody Contact contact) {
    return "Received";
}
ABecause the @Email annotation requires a custom validator bean to be registered.
BBecause @Email only works on fields of type java.net.Email, not String.
CBecause the email field is private and needs to be public for validation.
DBecause @Valid is missing on the controller method parameter, so validation is not triggered.
Attempts:
2 left
💡 Hint
Check how validation is triggered in Spring Boot controllers.
🧠 Conceptual
advanced
2:00remaining
How does @Pattern differ from @Email in validation?
Which statement best describes the difference between @Pattern and @Email annotations in Spring Boot validation?
A@Email validates only email format, while @Pattern can validate any string against a custom regex.
B@Pattern validates email format, while @Email validates phone numbers.
C@Email and @Pattern are interchangeable and validate the same patterns.
D@Pattern automatically fixes invalid input, @Email only reports errors.
Attempts:
2 left
💡 Hint
Think about the purpose of each annotation.
state_output
expert
2:00remaining
What is the validation error message when @Pattern fails?
Given this code snippet, what exact message will be returned if the zipCode does not match the pattern?
Spring Boot
public class Location {
    @Pattern(regexp = "\\d{5}", message = "ZIP code must be 5 digits")
    private String zipCode;

    // getters and setters
}

@PostMapping("/location")
public ResponseEntity<String> addLocation(@Valid @RequestBody Location location) {
    return ResponseEntity.ok("Location added");
}
A"Invalid ZIP code format"
B"ZIP code must be 5 digits"
C"zipCode does not match pattern"
D"Validation failed for zipCode"
Attempts:
2 left
💡 Hint
Look at the message attribute in the @Pattern annotation.