Bird
Raised Fist0
Spring Bootframework~10 mins

Custom validator annotation in Spring Boot - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of creating a custom validator annotation in Spring Boot?
easy
A. To define your own validation rules reusable across your application
B. To replace built-in annotations like @NotNull completely
C. To automatically generate database tables
D. To handle HTTP requests in controllers

Solution

  1. Step 1: Understand the role of custom validator annotations

    They allow you to create your own rules for validating data beyond built-in checks.
  2. Step 2: Identify the main benefit

    These annotations keep validation logic clean and reusable across different parts of your app.
  3. Final Answer:

    To define your own validation rules reusable across your application -> Option A
  4. Quick Check:

    Custom validator = reusable validation rules [OK]
Hint: Custom validators create reusable rules, not replace built-ins [OK]
Common Mistakes:
  • Thinking custom validators replace all built-in annotations
  • Confusing validation with database or HTTP handling
  • Assuming custom validators auto-generate code
2. Which of the following is the correct way to declare a custom validator annotation interface in Spring Boot?
easy
A. @Validator class MyValidator { String message() default "Invalid"; }
B. class MyValidator { String message() default "Invalid"; }
C. interface MyValidator { void validate(); }
D. @interface MyValidator { String message() default "Invalid"; Class[] groups() default {}; Class[] payload() default {}; }

Solution

  1. Step 1: Recall the syntax for custom annotation interfaces

    They use @interface keyword and define methods like message(), groups(), and payload().
  2. Step 2: Check each option

    @interface MyValidator { String message() default "Invalid"; Class[] groups() default {}; Class[] payload() default {}; } correctly uses @interface and includes required methods. Others either use wrong keywords or miss required parts.
  3. Final Answer:

    @interface MyValidator { String message() default "Invalid"; Class[] groups() default {}; Class[] payload() default {}; } -> Option D
  4. Quick Check:

    Custom annotation = @interface + standard methods [OK]
Hint: Custom annotations use @interface with message, groups, payload [OK]
Common Mistakes:
  • Using class or interface instead of @interface
  • Omitting groups() or payload() methods
  • Adding methods unrelated to validation
3. Given this validator class snippet, what will happen when validating a string with value "abc123"?
public class AlphaValidator implements ConstraintValidator<Alpha, String> {
  public boolean isValid(String value, ConstraintValidatorContext context) {
    return value != null && value.matches("^[a-zA-Z]+$");
  }
}
medium
A. Validation throws a NullPointerException
B. Validation passes because the string contains letters
C. Validation fails because the string contains digits
D. Validation always returns true regardless of input

Solution

  1. Step 1: Analyze the validation logic

    The method checks if the string is not null and matches the regex "^[a-zA-Z]+$", which means only letters allowed.
  2. Step 2: Test the input "abc123" against the regex

    Since "abc123" contains digits, it does not match the regex, so the method returns false.
  3. Final Answer:

    Validation fails because the string contains digits -> Option C
  4. Quick Check:

    Regex allows only letters, digits cause failure [OK]
Hint: Check regex carefully; digits break letter-only pattern [OK]
Common Mistakes:
  • Assuming partial match passes validation
  • Ignoring null check in code
  • Thinking digits are allowed by regex
4. You wrote a custom validator but it always passes validation even for invalid data. Which of these is the most likely cause?
medium
A. The annotation interface is missing the @Target annotation
B. The isValid method always returns true regardless of input
C. The validator class does not implement ConstraintValidator
D. The message() method in the annotation returns an empty string

Solution

  1. Step 1: Understand the role of isValid method

    This method contains the validation logic and must return true only for valid inputs.
  2. Step 2: Identify why validation always passes

    If isValid always returns true, invalid data will pass unchecked.
  3. Final Answer:

    The isValid method always returns true regardless of input -> Option B
  4. Quick Check:

    isValid controls validation result; always true means always pass [OK]
Hint: Check isValid method return values first when validation fails [OK]
Common Mistakes:
  • Forgetting to implement ConstraintValidator interface
  • Missing @Target causes compile warnings but not always validation failure
  • Empty message() affects error text, not validation logic
5. You want to create a custom validator annotation @StartsWith that checks if a string starts with a given prefix. Which combination of elements is required to implement this correctly?
hard
A. An annotation interface with a String prefix() method, a validator class implementing ConstraintValidator<StartsWith, String>, and overriding isValid to check the prefix
B. An annotation interface with int length(), a validator class implementing Validator, and overriding validate to check length
C. A class annotated with @Component that implements ConstraintValidator without an annotation interface
D. An annotation interface with String suffix(), a validator class implementing ConstraintValidator<EndsWith, String>, and overriding isValid to check suffix

Solution

  1. Step 1: Define the annotation interface with a prefix parameter

    The annotation must declare a method String prefix() to accept the prefix value.
  2. Step 2: Implement the validator class correctly

    The validator class must implement ConstraintValidator<StartsWith, String> and override isValid to check if the string starts with the given prefix.
  3. Final Answer:

    An annotation interface with a String prefix() method, a validator class implementing ConstraintValidator<StartsWith, String>, and overriding isValid to check the prefix -> Option A
  4. Quick Check:

    Annotation + validator class + isValid checking prefix = correct [OK]
Hint: Match annotation method and validator generic types carefully [OK]
Common Mistakes:
  • Using wrong method names like suffix() for prefix check
  • Implementing wrong interfaces or missing annotation interface
  • Confusing validate() with isValid() method names