Java EE6 Core Features: Bean Validation parsing __java

Source: Internet
Author: User
Tags java se

A core feature of the Bean Validation (JSR 303)--java EE 6, which defines a metadata model and API for entity validation. Its default metadata source is annotations, but developers can extend it through an XML descriptor. The Validation API does not rely on a particular application layer or programming model, so that the same set of validations can be shared by all tiers of the application. It also provides the mechanism for increasing the customized validation constraints by extending the validation API and the means to query constraints on the metadata warehouse.

Before the advent of JEE6 bean validation, developers had to write validation rules in the presentation-layer framework, business layer, and persistence layer to keep these rules synchronized, but this was a waste of time and error-prone.  Bean validation are implemented through constraints, which appear as annotations, which can be placed on the properties, methods, or classes of JavaBean (such as backing beans). Constraints can be either built-in annotations (located under the Javax.validation.constraints package) or user-defined. Some of the commonly used built-in annotations are listed below:
Min: The element that is annotated by @min must be a number whose value is greater than or equal to the given minimum value. Max: The element that is annotated by @max must be a number whose value is less than or equal to the given maximum value. Size: @Size indicates that the annotated element must be between the given minimum and maximum values. The data types that support size validation are string, Collection (size of the collection), map, and array. Notnull: @NotNull Ensure that the element being annotated cannot be null. Null: @Null ensure that the element being annotated must be null. Pattern: @Pattern Ensure that the annotated element (String) is bound to match the given Java regular expression.



The following example code declares some constraints through the bean validation Annotation: Java code public   class  Address {      & nbsp;    @NotNull   @Size (max=)            Private  String addressline1;              @Size ( max=           private  String addressline2;              public  string getaddressline1 ()  {              return  addressline1;          }              public   void  setaddressline1 (string addressline1)  {              this .addressline1 = addressline1;          }  }  

public class address {@NotNull @Size (max=30) private String addressline1;

       @Size (max=30) private String addressline2;
       Public String GetAddressline1 () {return addressline1;
       } public void SetAddressline1 (String addressline1) {this.addressline1 = addressline1; }
}

The



@NotNull Specifies that the annotated element addressline1 cannot be null; @Size specifies that the annotated element addressline1 and Addressline2 cannot exceed the given maximum value, which is 30 characters.

When the address object is validated, the value of the addressline1 is passed to the validation class for the @notnull constraint and to the validation class for the @size constraint, while the Addressline2 value is passed to the validation class for the @size constraint. Validated by the relevant validation class.

The following code customizes a constraint named ZipCode: Java code @Size (min= 5,  max= 5)       @ Constraintvalidator (Zipcodevalidator. Class)        @Documented         @Target ({Annotation_type, method, field})        @Retention (RUNTIME)       public   @interface  ZipCode {           string message ()  default   "Wrong zipcode";           string[] groups ()  default  {};  }  

 @Size (min=5, max=5) @ConstraintValidator (zipcodevalidator.class) @Documented @Target ({annotatio N_type, method, FIELD}) @Retention (RUNTIME) public @interface ZipCode {String message () default wrong Zipco
       De ";
String[] Groups () default {}; }

The



can use @zipcode on a class, property, or method, just like any other constraint. Java code public   class  Address {           @ZipCode           private  String zipCode;              public  string getzipcode ()  {              return  zipCode;           }             public   void   Setzipcode (string zipcode)  {              This .zipcode = zipcode;          }  }   

public class Address {
       @ZipCode
       private String ZipCode;

       Public String Getzipcode () {return
          zipCode;
       }

       public void Setzipcode (String zipCode) {
          this.zipcode = ZipCode;
       }
}



Validation API

Developers can programmatically validate JavaBean with the help of the validation API. The default package for the Bean Validation API is javax.validation. Some of the classes in the package are described below:

Constraintvalidator: This is an interface, and the specific constraint validation class needs to implement the interface. The interface defines the relevant logic to validate constraints in a given object type.

Validator: the Validahttp://java. Sun.com/javaee/6/docs/api/index.html?javax/validation/validator.htmltor interface holds the contract of an object validation diagram. The implementation of this interface must be thread-safe.

constraintviolation: The Constraintviolation interface represents a constraint validation failure on a given bean that exposes a constraint violation context and information that describes the violation.

validationexception: If some unrecoverable errors occur during the validation process, Validationexception exceptions are thrown. In some cases, the exception can be specified, such as an illegal grouping (group) definition, an illegal constraint definition, and an illegal constraint declaration, and so on.

constraint metadata Request API

The Bean validation specification provides a means of querying constraints on warehouses. This API is used primarily for tool support and integration with other frameworks, libraries, and JSR. The Bean validation specification is designed to provide a validation engine and a metadata warehouse for object constraints. Frameworks that require constraint definitions, validation, and metadata (Java EE or Java SE) can use the Bean Validation specification to accomplish these functions, from an application or infrastructure standpoint, to avoid unnecessary duplication of effort.

The Bean validation has been integrated into JSF 2.0 and JPA 2.0. In JSF, you can bind a form input field to the properties of a domain object. JSF 2 and bean validation can determine which property is bound and perform the validation associated with it, and display the constraint violation information to the user.

The Hibernate Validator 4 is a reference implementation framework for the Bean Validation specification, and its latest edition adds new features such as group validation, natural integration with JPA 2 and JSF 2, and extended annotation sets, among others.

Turn from: http://www.javaeye.com/news/15265

Related Article

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.