Jakarta Bean Validation API Tutorial and Examples in the Java Class Library

Jakarta Bean Validation API (Jakarta BV API) is an open source specification for data verification in the Java class library.It provides a lightweight and flexible way to verify the attribute values of Java Bean to ensure the effectiveness and consistency of the data.This article will introduce how to use the Jakarta BV API for data verification and provide some example code to help readers better understand. I. Overview When developing applications, data verification is an important step to ensure data integrity and reliability.By using the Jakarta BV API, developers can quickly and effectively check the data and take corresponding measures based on the results of the verification.The API provides a set of annotations and related verifications that can be applied to the attributes of Java Bean to define the verification rules in a statement. 2. Use the Jakarta BV API for data verification 1. First, you need to add the Jakarta BV API to the project.You can add the following dependencies in the construction configuration file (such as Maven's pom.xml): <dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> <version>3.0.0</version> </dependency> 2. In the Java Bean class that needs to be verified, use annotations provided by the Jakarta BV API to define the verification rules.For example, we can use the `@notnull` annotation to ensure that the value of the attribute is not NULL: public class User { @NotNull private String username; // Getters and setters... } 3. Before the data verification, you need to create a validator object.The verification device is responsible for the actual execution of the verification rules.The following is an example of creating a verification device and a data verification: ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); User user = new User(); user.setUsername(null); Set<ConstraintViolation<User>> violations = validator.validate(user); for (ConstraintViolation<User> violation : violations) { System.out.println(violation.getMessage()); } In the above example, we use the `VALIDATION.BUILDEFAULTVALIDATORDACTORY () method to create a default authentication factory, and obtain an authenticator instance through the` GetValidator () `method.Then, we created an object of the `user` and set the` username` property to `null`, and finally check the` user` object through the `value ()` method.The verification results will be encapsulated into a collection of `Constraintvilation" objects, and we can traverse the collection and output verification information. 4. In addition to `@notnull` annotations, the Jakarta BV API also provides many other annotations and verifications to handle various verification needs.Here are some commonly used annotations and examples of use: -`@Notblank`: The value of the verification string is not empty, and the length after the front and rear spaces are removed is not 0. -`@Notempty`: The value of the check collection, array or MAP is not empty. -`@SiZe`: The length of the verification string, collection, array or MAP is within the specified range. -`@Email`: check whether the string is a legal email address. -`@Pattern`: Whether the values of the regular expression of the test string meet the expectations. By adding corresponding annotations to the attributes that need to be verified, the verification rules can be easily defined. Third, sample code The following is a complete example of data verification using Jakarta BV API for data verification: import jakarta.validation.Validation; import jakarta.validation.Validator; import jakarta.validation.ValidatorFactory; import jakarta.validation.constraints.NotBlank; public class User { @NotBlank (Message = "Username cannot be empty") private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public static void main(String[] args) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); User user = new User(); user.setUsername(""); var violations = validator.validate(user); for (var violation : violations) { System.out.println(violation.getMessage()); } } } In the above example, we define a `user` class, which is marked by the` username` property.In the `main ()` method, we created a `user` object and set the` username` property to an empty string.Then, we used the verification device to check the `User` object and output the verification results. The above is a brief introduction and example of Jakarta Bean Validation API.By using the API, developers can easily check the attributes of Java Bean to ensure the effectiveness and consistency of the data.I hope this article will be helpful to you and use the Jakarta BV API!