Java EE 6ProposedBean ValidationStandard, with annotationsJavaBean constraints, not limited to a certain level or a programming model, flexible and easy to use. The following describes the features of this specification.
Overview Bean Validation specifications
Bean is the abbreviation of Java Bean. in the practical application of Java layered architecture, from the presentation layer to the persistence layer, each layer needs to verify the business compliance of Java Bean, as shown in 1. However, for objects of the same Java Bean, it is time-consuming and easy to induce errors when the same verification logic needs to be implemented at each layer. The goal of Bean Validation specifications is to avoid repeated multi-layer verification. In fact, developers prefer to put verification rules directly in Java Bean and use annotations to design verification rules.
Figure 1. Java layered verification structure
The JSR303 specification (Bean Validation Specification) provides a way to verify Java Beans in Java EE and Java SE. This specification mainly uses the annotation method to implement the Java Bean verification function, and this method will overwrite the authentication descriptor in XML format, so that the verification logic is separated from the Business Code, 2.
Figure 2. Java Bean Verification Model
The JSR303 specification provides an API that is a general extension of the Java Bean object model. It is not limited to a certain layer or programming model and can be used on both the server side and the client side, its biggest feature is ease of use and flexibility.
Hibernate Validator4.0 is one of the Reference implementations of the JSR303 specification. All sample codes in this article use this reference implementation.
Below is a simple example of Bean Validation (listing 1 ):
Listing 1:
- public class Employee {
- @NotNull(message = "The id of employee can not be null")
- private Integer id;
-
- @NotNull(message = "The name of employee can not be null")
- @Size(min = 1,max = 10,message="The size of employee's name must between 1 and 10")
- private String name;
-
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public static void main(String[] args) {
- Employee employee = new Employee();
- employee.setName("Zhang Guan Nan");
- ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
- Validator validator = vf.getValidator();
- Set > set = validator.validate(employee);
- for (ConstraintViolation constraintViolation : set) {
- System.out.println(constraintViolation.getMessage());
- }
- }
- }
The output result of running this example is:
- The size of employee's name must between 1 and 10
- The id of employee can not be null
As shown in the example, Bean Validation uses annotation (@ NotNull and @ Size) to constrain the declaration of the field id and name. When the Java Bean is actually used, the related validators verify the instances of this class to ensure that they comply with the constraints. Java Bean verification can be completed in the following four steps:
1. Definition of constraint Annotation
2. Constraints Verification rules (constraints validators)
3. constraint annotation Declaration
4. Constraints verification process
The second part of this article will detail the definition of the constraint annotation and the constraints verification rules. The third part will detail the description of the constraint annotation and the constraints verification process. The fourth part will introduce the APIS provided by the JSR303 standard.