Application Examples of the Validation Framework in Java Class Libraries)
Example application examples of verification framework based on Java library
Validation is a very important task in software development. It is used to check whether the input data, user operation or program status meet the expectations.There are some excellent verification frameworks in the Java class library, such as Hibernate Validator and JSR-303 Bean Validation, etc. They provide developers with powerful tools to simplify the realization of verification logic.
This article will introduce the application instance of the verification framework in the Java class library and provide relevant Java code examples.
一、Hibernate Validator
Hibernate Validator is a sub -project of the Hibernate project. It is a fast, scalable and customized Java Bean verification framework.Based on the standard JSR-303 Bean Validation specification, it provides many extension functions and custom verification annotations.Below is an example of using Hibernate Validator:
public class User {
@Notnull (Message = "Username cannot be empty")
@Size (min = 1, max = 20, message = "User name length must be between 1 and 20")
private String username;
@Notnull (Message = "Password cannot be empty")
@Size (min = 6, max = 20, message = "Password length must be between 6 and 20")
private String password;
// omit the getter and setter method
}
public class UserValidator {
public static void main(String[] args) {
User user = new User();
user.setUsername("");
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());
}
}
}
In the above example, the User class uses the verification annotations provided by the Hibernate Validator, adding Notnull and Size annotations to the Username and Password fields.In the UserValidator class, a User object is created, and it uses Hibernate Validator to verify it.The verification results will return a set that contains all violations of constraints. By traversing this collection, we can get messages of each verification error.
2, JSR-303 Bean Validation
JSR-303 Bean Validation is a specification in the Java standard library, which provides a set of APIs and annotations for verifying Java Bean.Below is an example of using JSR-303 Bean Validation:
public class Order {
@Notempty (MESSAGE = "Order Number cannot be empty")
private String orderNumber;
@Decimalmin (value = "0", inclusive = false, message = "order amount must be greater than 0")
private BigDecimal amount;
// omit the getter and setter method
}
public class OrderValidator {
public static void main(String[] args) {
Order order = new Order();
order.setOrderNumber("");
order.setAmount(new BigDecimal(0));
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Order>> violations = validator.validate(order);
for (ConstraintViolation<Order> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
In the above example, the Order class uses the verification annotation provided by the JSR-303 Bean Validation. The NoteMPTY annotation is added to the OrderNumber field, and the Decimalmin annotation is added to the Amount field.In the OrderValidator class, a Order object is created, and it uses JSR-303 Bean Validation to verify it.Similarly, through the collection of verification results, we can get the message of each verification error.
Summarize:
This article introduces the examples of the Java -based verification framework in practical applications, and provides related Java code examples.By using these verification frameworks, developers can simplify the writing of verification logic, improve development efficiency, and ensure the stability and security of the software system.I hope this article will help you understand and apply the verification framework!