Java EE 6 core features: Bean Validation features (1)

Source: Internet
Author: User

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:

 
 
  1. public class Employee {   
  2.  @NotNull(message = "The id of employee can not be null")   
  3.  private Integer id;   
  4.  
  5.  @NotNull(message = "The name of employee can not be null")   
  6.  @Size(min = 1,max = 10,message="The size of employee's name must between 1 and 10")   
  7.  private String name;   
  8.  
  9.  public int getId() {   
  10.  return id;   
  11.  }   
  12.  public void setId(int id) {   
  13.  this.id = id;   
  14.  }   
  15.  public String getName() {   
  16.  return name;   
  17.  }   
  18.  public void setName(String name) {   
  19.  this.name = name;   
  20.  }   
  21.  public static void main(String[] args) {   
  22.  Employee employee = new Employee();   
  23.  employee.setName("Zhang Guan Nan");   
  24.  ValidatorFactory vf = Validation.buildDefaultValidatorFactory();   
  25.  Validator validator = vf.getValidator();   
  26.  Set > set = validator.validate(employee);   
  27.  for (ConstraintViolation  constraintViolation : set) {   
  28.  System.out.println(constraintViolation.getMessage());   
  29.  }   
  30.  }   
  31.  }  

The output result of running this example is:

 
 
  1. The size of employee's name must between 1 and 10  
  2. 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.