Challenge - 5 Problems
Custom Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when validating a field with a custom annotation?
Consider a Spring Boot application with a custom annotation
@StartsWithA that validates if a string starts with the letter 'A'. What happens when a field annotated with @StartsWithA contains the value "Apple"?Spring Boot
public class Fruit { @StartsWithA private String name; // constructor, getters, setters } // Assume @StartsWithA is correctly implemented to check if the string starts with 'A'.
Attempts:
2 left
💡 Hint
Think about what the custom validator checks and the value given.
✗ Incorrect
The custom validator checks if the string starts with 'A'. Since "Apple" starts with 'A', validation passes.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom validator annotation in Spring Boot?
You want to create a custom annotation
@StartsWithA to validate strings starting with 'A'. Which option shows the correct annotation definition?Attempts:
2 left
💡 Hint
Remember that custom annotations for validation must specify target, retention, and the validator class.
✗ Incorrect
Option D correctly uses @Target, @Retention, and @Constraint with validatedBy, and defines required elements for a validation annotation.
🔧 Debug
advanced2:00remaining
Why does the custom validator not trigger during validation?
Given a custom annotation and validator, the validation does not trigger when saving an entity. What is the most likely cause?
Spring Boot
public class User { @StartsWithA private String username; // getters and setters } // Validator class and annotation are implemented correctly.
Attempts:
2 left
💡 Hint
Check how Spring Boot finds and uses validator classes.
✗ Incorrect
If the validator class is not properly linked or registered, Spring Boot won't run it during validation.
❓ state_output
advanced2:00remaining
What is the validation error message when the input fails the custom validator?
If a field annotated with
@StartsWithA(message = "Must start with A letter") contains "Banana", what error message will be shown?Attempts:
2 left
💡 Hint
Look at the message attribute in the annotation.
✗ Incorrect
The message attribute in the annotation overrides the default message and is shown on validation failure.
🧠 Conceptual
expert2:00remaining
Why use custom validator annotations instead of manual validation in Spring Boot?
Which is the best reason to create a custom validator annotation rather than manually checking values in service methods?
Attempts:
2 left
💡 Hint
Think about integration and reusability benefits.
✗ Incorrect
Custom validator annotations work with Spring's validation system, allowing automatic validation and consistent error handling across the app.