The steps and processes of data verification in the Java class library using Jakarta Bean Validation API
Using Jakarta Bean Validation API (previously referred to as JSR 303) can be very convenient to check the data to ensure the effectiveness and consistency of the data.This article will introduce steps and processes for data verification in the JAVA class library to use the Jakarta Bean Validation API, and provide the corresponding Java code example.
Step 1: Add dependencies
First of all, you need to add Jakarta Bean Validation API to the project construction tool (such as Maven or Gradle).Add the following dependencies to the pom.xml file of the Maven project:
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
Step 2: Create a physical class to be verified
Next, create a verified physical class.Assuming we build an application, users need to fill in the registry, including fields, emails and age.We create a physical class called User, such as:
public class User {
@NotNull
private String name;
@Email
private String email;
@Min(18)
private int age;
// omit the getter and setter method
}
In the above examples, we use some commonly used verification annotations: @Notnull is used to ensure that the field is not empty,@email is used to verify whether the field is an effective email address,@min is used to ensure that the value of the age field is greater than equal to equal18.
Step 3: Execution verification
Once we have a physical class to be verified, the verification operation can be performed at the appropriate position of the application.Generally, this will be performed before the form is submitted or data is saved.
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
public class Main {
public static void main(String[] args) {
// Create a verification device factory
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
// Create a physical object that needs to be verified
User user = new User();
user.setName(null);
user.setEmail("invalid_email");
user.setAge(16);
// Execute verification operation
Set<ConstraintViolation<User>> violations = validator.validate(user);
if (violations.isEmpty()) {
System.out.println ("Data verification pass!");
} else {
for (ConstraintViolation<User> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
}
In the above examples, we first created a verification factory factory through the Validation class, and then obtained a validator instance through the factory.Then, we created a User object and set the value that did not meet the verification rules.Finally, we call the value method to perform the verification operation and output the corresponding information according to the verification results.
Step 4: Treatment of verification results
In the processing stage of the verification results, we can take different operations as needed.In the above example, if the verification is passed, we will output a successful message.Otherwise, we will traverse the violation of the verification results and output the news of violation of restraint.
It should be noted that in practical applications, more flexible ways are usually used to process the verification results, such as displaying the error message on the front page page, or recording the error message in the log.
In summary, the steps of using the Jakarta Bean Validation API include: adding dependencies, creating a physical class that must be verified, performing verification operations and processing verification results.I hope the information provided in this article will help you understand and use the Jakarta Bean Validation API.
For a complete Java code example of the above content, please refer to the following links: [github gist] (https://gist.github.com/)