The most commonly used verification framework recommendation in the Java class library
In Java development, the correctness and integrity of verifying the input data is very important.In order to simplify the verification process, there are many commonly used verification frameworks in the Java library to choose from.This article will recommend some of the most commonly used Java verification frameworks and provide relevant example code.
1. Hibernate Validator:
Hibernate Validator is a powerful verification framework based on the Java Bean verification specification (JSR 380).It can verify the attributes of the object, provides rich annotations for constraints, and supports custom verification rules.The following is an example code that uses Hibernate Validator for data verification:
import javax.validation.constraints.*;
public class User {
@NotBlank (Message = "Username cannot be empty")
private String username;
@Email (Message = "Email format is incorrect")
private String email;
@Size (min = 6, max = 20, message = "Password length must be between 6 and 20 characters")
private String password;
// omit the getter and setter method
}
In the code above, `@notblank` annotations marked the` username` attribute, indicating that the attribute cannot be empty;@SIZE` Mark the `Password` attribute, indicating that the length of the attribute must be between 6 and 20 characters.
2. Apache Commons Validator:
Apache Commons Validator is a verification framework provided by the Apache Software Foundation.It provides a series of verification methods to verify the legality of common data types (such as mailboxes, URLs, Date, etc.).The following is an example code that uses Apache Commons Validator for data verification:
import org.apache.commons.validator.routines.EmailValidator;
public class User {
private String email;
// omit the getter and setter method
public boolean validateEmail() {
EmailValidator validator = EmailValidator.getInstance();
return validator.isValid(email);
}
}
In the above code, we use the `emailvalidator` class to verify the legitimacy of the mailbox.By calling the `iSvalid` method, we can determine whether the given mailbox is valid.
3. Spring Validation:
Spring Validation is a verification framework built in the Spring framework.It implements the JSR 303 (Bean verification specification) and provides rich annotations to verify the attributes of the object.The following is an example code that uses Spring Validation for data verification:
import org.springframework.validation.annotation.Validated;
@Validated
public class User {
@NotBlank (Message = "Username cannot be empty")
private String username;
@Email (Message = "Email format is incorrect")
private String email;
@Size (min = 6, max = 20, message = "Password length must be between 6 and 20 characters")
private String password;
// omit the getter and setter method
}
In the above code, we used the `@velidated` annotation at the class level, indicating that the class needs to be verified.Then, use the annotations of `@notblank`,`@email`, and `@size` to mark the attributes and specify the verification rules.
In addition to the above -mentioned framework, there are some other recommended Java verification frameworks, such as:
-BEAN VALIDATION: The reference implementation of the Java Bean verification specifications provides a standard framework for verifying the Java object.
-GOOGLE Guava Preconditions: A verification tool class in the Google Guava library for pre -conditions for verifying methods.
To sum up, the above -mentioned verification frameworks are very commonly used in Java development. It can help developers simplify the process of data verification and improve the reliability and reused code.According to specific project needs, you can choose the verification framework suitable for your own project for use.