Analysis of the "Verification Framework" framework in the Java class library
Analysis of the "Verification Framework" framework in the Java class library
I. Introduction
Verification is an indispensable part of software development, which is used to ensure the legitimacy and consistency of data.In Java applications, the input data is usually required, such as form data entered by the user, parameters transmitted by API interface, and so on.In order to simplify the work of developers, the Java class library provides many verification frameworks for easy data verification.This article will analyze the common "verification framework" in the Java library and provide the corresponding example code.
2. Common Java verification framework
1. Hibernate Validator
Hibernate Validator is a verification framework based on the Java Bean verification specification (JSR 380). It provides a series of annotations and APIs for verification of Java objects.By adding annotations to attributes, developers can define various verification constraints, such as non -empty, length, regular expression matching.Using Hibernate Validator can easily verify the data entered by the user or the parameters passed by the API interface, and provide a friendly error prompt.The following is the example code of Hibernate Validator:
public class User {
@Notnull (Message = "Username cannot be empty")
private String username;
@Email (Message = "Email format is incorrect")
private String email;
@Size (min = 6, max = 12, message = "Password length must be between 6-12")
private String password;
// omit the getter and setter method
}
public class Main {
public static void main(String[] args) {
User user = new User();
// Set the data input by the user
user.setUsername(null);
user.setEmail("abc");
user.setPassword("123");
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
for (ConstraintViolation<User> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
2. Apache Commons Validator
Apache Commons Validator is an open source project of the Apache Software Foundation. It provides a series of verification and verification rules for verification of data.It supports various data type verifications, such as numbers, dates, mailboxes, URLs, etc.By using Apache Commons Validator, developers can easily verify the data entered by the user or parameters passed by API interface.The following is an example code of Apache Commons Validator:
public class Main {
public static void main(String[] args) {
String email = "abc";
EmailValidator validator = EmailValidator.getInstance();
if (validator.isValid(email)) {
System.out.println ("correct email format");
} else {
System.out.println ("Email format is incorrect");
}
}
}
3. JSR 303 - Bean Validation
The JSR 303 is a standard specification of the Java platform, defining a set of interfaces and annotations for verification of Java objects.By using JSR 303 related annotations, developers can easily verify the Java objects.For example,@notnull annotations are used to verify that the attribute is not empty,@siZe annotation is used to verify the length of the string, etc.The following is an example code that uses JSR 303 to verify:
public class User {
@Notnull (Message = "Username cannot be empty")
private String username;
@Email (Message = "Email format is incorrect")
private String email;
@Size (min = 6, max = 12, message = "Password length must be between 6-12")
private String password;
// omit the getter and setter method
}
public class Main {
public static void main(String[] args) {
User user = new User();
// Set the data input by the user
user.setUsername(null);
user.setEmail("abc");
user.setPassword("123");
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
for (ConstraintViolation<User> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
3. Summary
Verification is an important part of ensuring data legitimacy and consistency. In Java applications, data or API interface parameters are often required in Java applications.The Java class library provides many verification frameworks, such as Hibernate Validator, Apache Commons Validator, and JSR 303 for easy data verification.These verification frameworks help developers simplify data verification work by providing functions such as annotations, APIs, and verifications.Developers can choose the appropriate verification framework according to specific needs, and use them according to the document and example code provided by the framework.
Fourth, reference
-Hibernate validator official document: https://docs.jboss.org/hibernate/stable/validator/reference/en-us/html_single/
-Apache Commons value official website: https://commons.apache.org/proper/commons- value/
-JSR 303 official document: https://beanvalidation.org/1.0/spec/
-JSR 380 official document: https://beanvalidation.org/2.0/spec/