How to integrate Jakarta Bean Validation API in the Java Class library and conduct custom verification verification
Integrate Jakarta Bean Validation API in the Java library and conduct custom verification verification
Introduction:
Jakarta Bean Validation API is a open source framework for Java to verify the constraints of JavaBean.It can help us verify the data at runtime to ensure the correctness and integrity of the data.This article will introduce how to integrate Jakarta Bean Validation API in the Java class library and show how to use custom annotations for data verification.
step:
1. Import related dependencies:
First of all, we need to introduce the dependence of Jakarta Bean Validation API in the project.You can add the following dependencies to the pom.xml file:
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
2. Create the JavaBean class to be verified:
Next, we create a JavaBean class to be verified.Suppose we need to verify the name of a user, then we can create a User class that contains a name field.
public class User {
@Notblank (Message = "Name cannot be empty")
private String name;
// omit other fields and methods
}
In the above example, we used @Notblank annotations to verify that the name section is not empty.
3. Create a verification device class:
We need to create a verification class to perform the verification operation.Create a class called Validator and define a value method to perform verification.
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import java.util.Set;
public class Validator {
public static <T> void validate(T object) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<T>> violations = validator.validate(object);
for (ConstraintViolation<T> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
In the above example, we use value.builddefaultValidatorFactory (). GetValidator () method to obtain a default verification object.We use the verification object to verify the verification object and print the verification results.
4. Verification:
Finally, we can use the Validator class in the program for verification.Where we want to use the verification device, call the Validate method and pass the object to be verified.
public class Main {
public static void main(String[] args) {
User user = new User();
user.setName("");
Validator.validate(user);
}
}
In the above example, we created a User object and set the name field to empty.We then call the value of the Validator class to verify the object.
Custom verification:
In addition to using built -in verification annotations, we can also customize verification annotations.The following is an example, demonstrating how to customize a @validemail annotation to verify the mailbox address.
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Pattern (regexp = ".+@.+\\ ..+", message = "Please enter valid mailbox address")
@Size(max = 50)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface ValidEmail {
String message () default "Please enter the valid email address";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
In the above examples, we use @pattern and @size annotations to set the verification rules for mailbox addresses.Then, we created a @validemail annotation and used the @Constraint annotation to associate it with the verification annotation.
Through the above steps, we can integrate Jakarta Bean Validation API in the Java class library and use conventional and custom annotations for data verification.This can greatly simplify our code and improve the readability and maintenance of the program.