Common annotation of Java Ee Validation framework
Common annotation of Java Ee Validation framework
The Java Ee Validation framework provides a set of powerful annotations to verify the effectiveness of input data in application development.These annotations are based on the JSR-303 specifications and can be applied to various Java types and fields.This article will introduce a common annotation of the Java Ee Validation framework and provide related example code.
1. @NotNull
@Notnull annotation is not null for verification field values.It can be applied to the fields of string, collection, array and object type.
Example:
public class User {
@NotNull
private String username;
// getter and setter
}
2. @Size
@Size annotation is used to verify whether the length of the field is within the specified range.It can be applied to the fields of string, collection and array types.
Example:
public class User {
@Size(min = 2, max = 20)
private String username;
// getter and setter
}
3. @Pattern
@Pattern annotation is used to verify whether the field value matches the specified regular expression.
Example:
public class User {
@Pattern(regexp = "[a-zA-Z0-9]+")
private String username;
// getter and setter
}
4. @min and @Max
@Min and @Max annotations are used to verify the minimum and maximum values of digital type fields.
Example:
public class User {
@Min(18)
private int age;
// getter and setter
}
5. @Email
@Email annotation is used to verify the format of an email address in accordance with the value of the field.
Example:
public class User {
@Email
private String email;
// getter and setter
}
6. @asserttrue 和 @assertfalse
@Asserttrue and @AssertFalse annotations are used to verify whether the field values are true and false.
Example:
public class User {
@AssertTrue
private boolean active;
// getter and setter
}
7. @NotBlank
@NotBlank annotation is used to verify whether the string field value is empty and the length is greater than 0.
Example:
public class User {
@NotBlank
private String name;
// getter and setter
}
8. @NotEmpty
@Notempty annotation is used to verify whether the values of string, collection, and array are empty.
Example:
public class User {
@NotEmpty
private List<String> hobbies;
// getter and setter
}
Summarize:
By using these commonly used annotations provided by the Java Ee value framework, we can easily verify the effectiveness of the input data and improve the stability and security of the application.It is hoped that this article can help readers better understand and apply these annotations, and play a role in the actual Java EE project.