JSR303 does not correspond to the specified jar package, but a specification. At present, hibernate validator is the most used implementation based on JSR303 specification
This article is not suitable for newcomers. It is required to know at least how to use it
Springboot integrates the starter launcher of JSR303
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
Normally reference the dependency of Hibernate validator
<!-- 6.2.0.Final Is the recommended version --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.2.0.Final</version> </dependency>
Common annotation records
Some common annotations are recorded here, not all of which will be supplemented at any time
Notes brief introduction
Each annotation contains common attributes. Here we introduce message and groups:
annotation | effect |
---|---|
message | String, the error message prompted after an error is reported |
groups | The class attribute of the interface is the verification group |
Value type verification
annotation | effect |
---|---|
@Null | Must be Null |
@NotNull | Must not be Null |
@NotBlank | Must not be empty (excluding the first space) |
@NotEmpty | The effect is equivalent to @ NotBlank |
@AssertTrue | Must be true |
@AssertFalse | Must be false |
@Pattren | Must conform to the regular expression |
Must be in email format |
Range type verification
annotation | effect |
---|---|
@Min | Must be a number and greater than or equal to the specified number |
@Max | Must be a number and less than or equal to the specified number |
@Range | Must be a number and greater than or equal to min and less than or equal to max |
@DecimalMin | Must be a number and greater than or equal to the specified number |
@DecimalMax | Must be a number and less than or equal to the specified number |
@Digits | It must be a number, and the integer digit cannot exceed integer bit, and the decimal digit cannot exceed fraction bit |
@Length | Must be a string greater than or equal to min and less than or equal to max |
@Size | Applicable to string collection array, the length is greater than or equal to min and less than or equal to max |
@Past | Must be a past date |
@Future | Must be a future date |
Custom verification annotation
To customize a verification annotation, you need to create an annotation, and then add three basic attributes that each verification annotation has
@Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) public @interface Phone { String message() default ""; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; }
Now the annotation is created, but it is a useless empty shell annotation. If you want it to have the ability of verification, you need a class to help him complete the verification. Use the @ Constraint annotation to specify which class to help him complete the verification
@Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) // Indicates that you want to use PhoneValidate to complete the verification for yourself @Constraint(validatedBy = PhoneValidate.class) public @interface Phone { ... }
Next, create a PhoneValidate class to help the annotation complete the verification rules
// This class must implement the ConstraintValidator interface. Phone in the generic type represents the effective annotation, and String represents the type of the annotated attribute public class PhoneValidate implements ConstraintValidator<Phone, String> { // private String pattern; @Override public void initialize(Phone annotation) { // Here, the execution priority is high. The value carried in the annotation can be obtained through annotation for subsequent verification processing, for example: // this.pattern = annotation.pattern(); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { // Determine whether the verification passes by returning true or false String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])|(18[0-9])|(19[8,9]))\\d{8}$"; return value.matches(regExp); } }
This completes the custom validation annotation