0
0
Spring Bootframework~10 mins

Custom validator annotation 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 define a custom annotation for validation.

Spring Boot
public @interface [1] {
    String message() default "Invalid value";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
Drag options to blanks, or click blank then click option'
ACustomValidator
BConstraint
CValidCustom
DMyValidator
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'Constraint' which is a meta-annotation, not a custom annotation name.
Using names that do not start with 'Valid' which is a common convention.
2fill in blank
medium

Complete the code to add the correct meta-annotation to define a custom validator annotation.

Spring Boot
@[1](validatedBy = CustomValidator.class)
public @interface ValidCustom {
    String message() default "Invalid value";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
Drag options to blanks, or click blank then click option'
AConstraint
BTarget
CRetention
DDocumented
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Target or @Retention instead of @Constraint for linking validator.
Omitting the validatedBy attribute.
3fill in blank
hard

Fix the error in the validator class by completing the method signature correctly.

Spring Boot
public class CustomValidator implements ConstraintValidator<ValidCustom, String> {
    @Override
    public boolean [1](String value, ConstraintValidatorContext context) {
        return value != null && value.matches("^[a-zA-Z]+$");
    }
}
Drag options to blanks, or click blank then click option'
Avalidate
BisValid
Ccheck
DvalidateValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'validate' or 'check' which are not the correct method names.
Incorrect method signature causing compile errors.
4fill in blank
hard

Fill both blanks to specify where the custom annotation can be applied and its retention policy.

Spring Boot
@Target([1])
@Retention([2])
@Constraint(validatedBy = CustomValidator.class)
public @interface ValidCustom {
    String message() default "Invalid value";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
Drag options to blanks, or click blank then click option'
AElementType.FIELD
BElementType.METHOD
CRetentionPolicy.RUNTIME
DRetentionPolicy.CLASS
Attempts:
3 left
💡 Hint
Common Mistakes
Using RetentionPolicy.CLASS which is not available at runtime.
Targeting methods only when fields are intended.
5fill in blank
hard

Fill all three blanks to create a map of field names to their lengths for words longer than 3 characters.

Spring Boot
Map<String, Integer> lengths = words.stream()
    .filter(word -> word.length() [1] 3)
    .collect(Collectors.toMap(
        word -> word.[2](),
        word -> word.[3]()
    ));
Drag options to blanks, or click blank then click option'
A>
Blength
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the filter.
Using incorrect methods for key or value mapping.