Javaee6 core features: beanvalidation Analysis

Source: Internet
Author: User
Tags java se

Bean validation is a new framework for data verification in Java EE 6. The validation API does not depend on a specific application layer or programming model, so that the same set of verification can be shared by all layers of the application. It also provides a mechanism to add custom verification constraints by extending the validation API and a means to query the restricted metadata warehouse.

Before bean validation of Java EE 6 appeared, developers had to write verification rules in the presentation layer framework, business layer, and persistence layer to ensure the synchronization of these rules, however, this is a waste of time and error-prone. Bean validation is implemented by constraints. These constraints appear in the form of annotations. Annotations can be placed on attributes, methods, or classes of JavaBean (such as backing bean. The constraint can be either a built-in annotation (located under the javax. validation. Constraints package) or user-defined. Some common built-in annotations are listed as follows:

◆ Min: the element annotated by @ Min must be a number whose value must be greater than or equal to the given minimum value.

◆ MAX: the element annotated by @ Max must be a number whose value is smaller than or equal to the given maximum value.

◆ Size: @ size indicates that the annotated element must be between the given minimum and maximum values. Data Types that support size verification include string, Collection (size of the computing set), map, and array.

◆ Notnull: @ notnull ensure that the element to be annotated cannot be null.

◆ NULL: @ null ensure that the element to be annotated must be null.

◆ Pattern: @ pattern ensure that the annotated element (string) matches the given Java regular expression.

Some constraints are declared in the code using the bean validation annotation:

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;           }       } 

@ Notnull specifies that the element addressline1 to be annotated cannot be null; @ size specifies that the elements addressline1 and addressline2 to be annotated cannot exceed the given maximum value, that is, 30 characters.

When verifying the address object, the value of addressline1 is passed to the verification class for the @ notnull constraint and the validation class for the @ size constraint, the value of addressline2 is passed to the verification class for the @ size constraint and verified by the related verification class. The following code defines a constraint named zipcode:

@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 {};       }

public class Address {           @ZipCode           private String zipCode;             public String getZipCode() {              return zipCode;           }             public void setZipCode(String zipCode) {              this.zipCode = zipCode;           }       } 

Validation API

Developers can verify JavaBean programmatically by using the validation API. The default bean validation API package is javax. validation. The following describes some classes in the package:

Constraintvalidator: This is an interface, which must be implemented by the Constraints Verification class. This interface defines the relevant logic to verify the constraints in the given object type.

Validator: validahttp: // java.sun.com/javaee/6/docs/api/index.html? The javax/validation/validator.html tor interface holds the contract of the object verification diagram. The implementation of this interface must be thread-safe.

Constraintviolation: The constraintviolation interface indicates that the constraint verification on the given bean fails. It discloses the context of the constraint violation and information describing the violation.

Validationexception: if some unrecoverable errors occur during verification, a validationexception is thrown. In some cases, this exception can be specified, such as the definition of an invalid group, the definition of an invalid constraint, and the declaration of an invalid constraint.

Constraint metadata request API

The bean validation specification provides a means to query the restricted warehouse. This API is mainly used for tool support and integration with other frameworks, libraries, and JSR. The bean validation specification aims to provide a verification engine and metadata repository for object constraints. The Framework (Java EE or Java SE) that requires constraint definition, verification, and metadata can use the bean validation specification to implement these functions. From the perspective of applications or infrastructure, this can avoid unnecessary repetitive work.

Bean validation has been integrated into JSF 2.0 and JPA 2.0. In JSF, You can bind the form input field to the attribute of the Field object. JSF 2 and bean validation can determine which property to bind and perform related verification. They will also display information about the violation of the constraint to the user. Hibernate validator 4 is a reference implementation framework of bean validation specifications. The latest version of hibernate validator 4 has many new features, such as grouping verification, natural integration with JPA 2 and JSF 2, and extended annotation sets.

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.