Performance: Custom validator annotation
This affects the runtime validation process during form submission or API request handling, impacting interaction responsiveness.
Jump into concepts and practice - no test required
public class SimpleValidator implements ConstraintValidator<CustomCheck, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { // Simple check without external calls return value != null && value.matches("^[a-zA-Z0-9]+$"); } }
public class HeavyValidator implements ConstraintValidator<CustomCheck, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { // Heavy operation like database call or complex calculation return database.exists(value); } }
| Pattern | Validation Time | Server Load | User Interaction Delay | Verdict |
|---|---|---|---|---|
| Heavy validation with DB calls | 100+ ms | High | High delay in response | [X] Bad |
| Simple regex validation | <1 ms | Low | Minimal delay | [OK] Good |
custom validator annotation in Spring Boot?@interface keyword and define methods like message(), groups(), and payload().@interface and includes required methods. Others either use wrong keywords or miss required parts.public class AlphaValidator implements ConstraintValidator<Alpha, String> {
public boolean isValid(String value, ConstraintValidatorContext context) {
return value != null && value.matches("^[a-zA-Z]+$");
}
}isValid always returns true, invalid data will pass unchecked.@StartsWith that checks if a string starts with a given prefix. Which combination of elements is required to implement this correctly?String prefix() to accept the prefix value.ConstraintValidator<StartsWith, String> and override isValid to check if the string starts with the given prefix.