0
0
Spring Bootframework~20 mins

Custom validator annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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'.
AValidation passes because "Apple" starts with 'A'.
BValidation fails because custom annotations require manual validation calls.
CValidation fails because "Apple" is not exactly 'A'.
DValidation passes only if the field is public.
Attempts:
2 left
💡 Hint
Think about what the custom validator checks and the value given.
📝 Syntax
intermediate
2: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?
A
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = StartsWithAValidator.class)
public class StartsWithA {
    String message() default "Must start with A";
}
B
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@Constraint(validatedBy = StartsWithAValidator.class)
public @interface StartsWithA {
    String message() default "Must start with A";
}
C
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface StartsWithA {
    String message() default "Must start with A";
}
D
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = StartsWithAValidator.class)
public @interface StartsWithA {
    String message() default "Must start with A";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
Attempts:
2 left
💡 Hint
Remember that custom annotations for validation must specify target, retention, and the validator class.
🔧 Debug
advanced
2: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.
AThe annotation is missing the @Documented meta-annotation.
BThe field username is private and needs to be public for validation.
CThe validator class is not registered as a Spring bean or not referenced correctly in the annotation.
DThe validation only works on controller methods, not on entity fields.
Attempts:
2 left
💡 Hint
Check how Spring Boot finds and uses validator classes.
state_output
advanced
2: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?
AMust start with A
BMust start with A letter
CValidation failed for field username
DBanana is invalid
Attempts:
2 left
💡 Hint
Look at the message attribute in the annotation.
🧠 Conceptual
expert
2: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?
ACustom annotations integrate with Spring's validation framework, enabling automatic checks and error reporting.
BManual validation is faster and more reliable than annotations.
CCustom annotations require less code but cannot be reused.
DManual validation is the only way to validate complex objects.
Attempts:
2 left
💡 Hint
Think about integration and reusability benefits.