Overview
Before Java EE 6 bean validation appeared, developers had to write validation rules in the presentation Layer framework, business layer/persistence layer to ensure the synchronization of these rules, but it was very time-consuming and error-prone. Bean validation is implemented by constraints, which appear as annotations that can be placed on properties, methods, or classes of JavaBean (such as backing beans). constraints can be either built-in annotations (under the Javax.validation.constraints package) or user-defined.
Refer to the Bean Validate standard specification document: https://jcp.org/aboutJava/communityprocess/final/jsr303/index.html
JSR303 built-in constraint rules
@AssertTrue/@AssertFalse Validation applicable field: Boolean Note description: Verify that the value is True/false Property Description:-@DecimalMax/@DecimalMin validation applicable field: BigDecimal, Biginteger,string,byte,short,int,long Note Description: Verify that the value is less than or equal to the specified decimal value, be aware of the precision problem with decimals attribute description: Common @digits validation applicable field: BigDecimal, Biginteger,string,byte,short,int,long Note Description: Verify that the numeric composition of the value is a legitimate property description: Integer: Specifies the number of digits in the integer part. Fraction: Specifies the number of digits in the fractional part. @Future/@Past validation applicable field: Date,calendar Note Description: Verify that the value is after/before the current time property Description: Common @max/@Min validation applicable field: Bigdecimal,biginteger,string,byte, Short,int,long Note Description: Verify that the value is less than or equal to the specified integer value property Description: Common @notnull/@Null validation applicable field: Reference data type note Description: Verify that the value is non-null/empty Property Description: Public @pattern Validation applicable field: String Note Description: Verify that the value is equipped with a regular Expression property Description: RegExp: Regular Expression Flags: Specifies an array of pattern.flag that represents the relevant options for the regular expression. @Size Validation applicable field: String,collection,map, array annotation Description: Verify that the value meets the length requirement property Description: Max: Specifies the maximum length, min: Specifies the minimum length. @Valid Validation applicable field: Reference type note Description: Verify that the value requires a recursive validation property description: None
Instance:
@Entity @table (name = "Agent") @SequenceGenerator (name = "Seq_gen", Sequencename = "seq_agent") public class Agententity Extends IdEntity {@NotBlank (message = "Name cannot be empty") @Size (max = $, message = "name length exceeds") @Pattern (regexp = "^[\\u 4e00-\\u9fa5\\w\\d_]*$ ", message =" name does not conform to specification ") private String name; @NotBlank (message = "codename cannot be Empty") @Size (max = +, message = "designator length exceeds") @Pattern (regexp = "^[0-9a-za-z_]*$", message = " Code name does not conform to the specification ") private String code; Private Agentstatusenum status; Private String description; Private Long Createtime; Private String CreateUser; Private Long UpdateTime; Private String UpdateUser; Private Long lastheartbeat; Public agententity () {super (); This.status = Agentstatusenum.offline; } @Column (name = "name") public String GetName () {return name; } public void SetName (String name) {this.name = name; } @Column (name = "code") public String GetCode () {return code; } public void Setcode (String code) {this.code = code; } @Enumerated (enumtype.string) public agentstatusenum getStatus () {return status; } public void SetStatus (Agentstatusenum status) {this.status = status; } public String GetDescription () {return description; } public void SetDescription (String description) {this.description = description; } @Column (name = "Create_time") public Long Getcreatetime () {return createtime; } public void Setcreatetime (Long createtime) {this.createtime = Createtime; } @Column (name = "Create_user") public String Getcreateuser () {return createUser; } public void Setcreateuser (String createUser) {this.createuser = CreateUser; } @Column (name = "Update_time") public Long Getupdatetime () {return updatetime; } public void Setupdatetime (Long updatetime) {this.updatetime = UpdateTime; } @Column (Name= "Update_user") public String Getupdateuser () {return updateUser; } public void Setupdateuser (String updateUser) {this.updateuser = UpdateUser; } @Column (name = "Last_heart_beat") public Long Getlastheartbeat () {return lastheartbeat; } public void Setlastheartbeat (Long lastheartbeat) {this.lastheartbeat = Lastheartbeat; }}
--------------------------------------------------------------------------------------------------------------- ----
Supplemental: Validate API
The following excerpt from: http://developer.51cto.com/art/201003/190130.htm
Validation API
Developers can use the validation API to programmatically validate JavaBean. 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 in which the specific constraint validation class needs to implement the interface. The interface defines the related logic to validate constraints in a given object type.
The Validator:validahttp://java.sun.com/javaee/6/docs/api/index.html?javax/validation/validator.htmltor interface holds the contract for the object validation diagram. The implementation of this interface must be thread-safe.
The Constraintviolation:constraintviolation interface indicates that a constraint validation failure on a given bean exposes a constraint violation context and information describing the violation.
Validationexception: If some unrecoverable errors occur during the validation process, a Validationexception exception is thrown. In some cases, you can specify the exception, such as an illegal grouping (group) definition, an illegal constraint definition, an illegal constraint declaration, and so on.
Constraint metadata Request API
The Bean validation specification provides a means of querying a constrained warehouse. The API is primarily used 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 definition, validation, and metadata (Java EE or Java SE) can do so with the bean validation specification, which avoids unnecessary duplication of effort, either from an application or infrastructure point of view.
Bean validation has been integrated into JSF 2.0 and JPA 2.0. In JSF, you can bind the 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 also display the information that the constraint violates to the user. Hibernate Validator 4 is the reference implementation framework for the Bean Validation specification, and its latest version adds a number of new features, such as group validation, natural integration with JPA 2 and JSF 2, and extended annotation sets, among others.
Javaee6 Core features--bean validation Framework (collation)