1. Import dependencies first
```java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>Copy the code
2. Add comments to the parameters to be verified
```java
public class LoginVo {
@NotNull
@IsMobile
private String mobile;
@NotNull
@Length(min=32)
private String password;
Copy the code
}
3. Add the @valid annotation to the position where parameters are to be verified
4. How to customize the verification annotation, can refer to the source of the existing annotation
1. Create a package for authentication
2. Refer to existing annotations that contain at least message(), groups(), and payload().
```java @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @Constraint(validatedBy = {IsMobileValidator.class }) public @interface IsMobile { boolean required() default true; String message() default "The phone number format is wrong "; Class<? >[] groups() default { }; Class<? extends Payload>[] payload() default { }; }Copy the code
Define define annotation validator class, inherit ConstraintValidator class, overwrite two methods (Initialize, isValid)
```java public class IsMobileValidator implements ConstraintValidator<IsMobile, String> { private boolean required = false; / / initialize the public void the initialize (IsMobile constraintAnnotation) {required = constraintAnnotation. Required (); } public boolean isValid(String value, ConstraintValidatorContext context) { if(required) { return ValidatorUtil.isMobile(value); }else { if(StringUtils.isEmpty(value)) { return true; }else { return ValidatorUtil.isMobile(value); // Validate logic}}}}Copy the code