Spring-boot读书笔记一弄懂ConstraintValidatorPassword, String

In ConstraintValidator<Password, String>, the two generic type parameters are:

First parameter: Password

  • This is the annotation type that this validator handles
  • It's a custom validation annotation (like @Password) that you would create
  • When someone uses @Password on a field, this validator will be called
  • Example: @Password private String userPassword;

Second parameter: String

  • This is the data type of the value being validated
  • It tells the validator what type of object it will receive in the isValid() method
  • In this case, it expects a String value (the password)
  • The isValid(String password, ...) method parameter matches this type

How it works:

  • You create a @Password annotation
  • Apply it to a String field: @Password String myPassword
  • When validation runs, this validator receives the String value
  • The isValid() method validates that String and returns true/false

Other examples:

  • ConstraintValidator<Email, String> - validates email strings
  • ConstraintValidator<Min, Integer> - validates minimum integer values
  • ConstraintValidator<NotNull, Object> - validates any object is not null

The generic types ensure type safety between the annotation and the value being validated.