JSR 303 Verification British Framework method
How to use the JSR 303 verification constraint framework
JSR 303 is a specification for Java Bean verification. It provides a simple and scalable method to verify the attributes of the Java object.This specification defines a set of verification constraints that can be applied to attribute values, and the engines that verify these constraints.
The following is the use of JSR 303 verification constraint framework:
1. Add dependencies:
First, you need to add JSR 303 to the dependency item of the JSR 303 verification constraint framework.You can use Maven or Gradle to add dependencies.Below is an example of Maven configuration:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
2. Create a Java Bean class that needs to be verified:
Create a Java class in your project, which contains the attributes to be verified.For example, we create a Java Bean class called Person and contain a attribute called AGE:
public class Person {
@Min (Value = 18, Message = "Age must be greater than or equal to 18 years"))
private int age;
// Construct function, Getter and Setter omit
}
In the above example, we use the @Min annotation to limit the minimum value of the AGE attribute to over 18 years old.
3. Use verification constraint annotation:
In your Java Bean class, use the JSR 303 verification constraint annotation to restrict attributes.Different annotations can be used to achieve different verification rules.Here are some commonly used verification constraints:
-@Notnull: Check whether the attribute value is null.
-@Size: Check the length of the attribute value.
-@Email: Check whether the attribute value is an effective email address.
-@Pattern: Use regular expressions to match the attribute value.
- etc.
You can choose appropriate annotations to restrain attributes according to your needs.
4. Verify in the method:
When you want to verify a Java Bean object, you can use the verification engine to verify in the method.The following is an example method, demonstrating how to use the JSR 303 verification constraint framework to verify a Person object:
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setAge(16);
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(person);
if (violations.isEmpty()) {
System.out.println ("Verification Pass");
} else {
for (ConstraintViolation<Person> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
}
In the above example, we first created a Person object and set up the AGE attribute values (16 years old) with dissatisfaction conditions.Then, we created a validatorFactory instance through value by validation.BuilddefaultValidatorFactory () and used it to create a Validator instance.We then use the validator () method of the value instance to verify the Person object.If the verification is passed, the print "verification" will pass the message.Otherwise, the news of the illegal regulations will be printed.
Summarize:
The JSR 303 verification constraint framework is a convenient and scalable method to verify the attributes of the Java object.By adding verification constraints and using a verification engine, we can easily verify the Java Bean object.This specification provides many verification and constraint annotations, which can choose appropriate annotations to restrain attributes according to needs.I hope this article can help you understand how to use the JSR 303 verification constraint framework.
Reference link:
- [Bean Validation - Using the constraints](https://beanvalidation.org/2.0/spec/#using)
- [Common Annotations for the JavaTM Platform Specification (JSR 250)](https://jcp.org/en/jsr/detail?id=250)