Hibernate Validator is a reference implementation of Bean Validation. Hibernate Validator provides the implementation of all the built-in constraint in the JSR 303 specification, in addition to some additional constraint.
In daily development, Hibernate Validator is often used to validate bean fields, based on annotations, for quick and efficient use.
1. Constraint built into the Bean Validation
Annotation effect
@Valid |
The annotated element is an object that needs to be checked for all field values of this object |
@Null |
The annotated element must be null |
@NotNull |
Annotated element must not be null |
@AssertTrue |
The annotated element must be true |
@AssertFalse |
Annotated element must be false |
@Min (value) |
The annotated element must be a number whose value must be greater than or equal to the specified minimum value |
@Max (value) |
commented Meta Must be a number whose value must be less than or equal to the specified maximum |
@DecimalMin (value) |
The annotated element must be a number whose value must be greater than or equal to the specified minimum |
@DecimalMax (value) |
The annotated element must be a number whose value must be less than or equal to the specified maximum |
@Size (max, min) |
commented The size of the element must be within the specified range |
@Digits (integer, fraction) |
The annotated element must be a number whose value must be within an acceptable range |
@Past |
Annotated element must be a past date |
@Future |
Annotated element must be a future date |
@Pattern (value) |
The annotated element must conform to the specified regular expression |
2. Hibernate Validator Additional constraint
Annotations |
function |
@Email |
The annotated element must be an e-mail address |
@Length (min=, max=) |
The size of the annotated string must be within the specified range |
@NotEmpty |
The annotated string must be non-empty |
@Range (min=, max=) |
The annotated element must be within the appropriate range |
@NotBlank |
The annotated string must be non-empty |
@URL (protocol=, Host=, port=, regexp=, flags=) |
The annotated string must be a valid URL |
@CreditCardNumber |
The annotated string must pass the Luhn checksum algorithm, Bank cards, credit cards and other numbers are generally used Luhn Computational legitimacy |
@ScriptAssert (lang=, script=, alias=) |
To have a Java Scripting API that is JSR 223 ("Scripting for the JavaTM Platform") |
@SafeHtml (Whitelisttype=, additionaltags=) |
Classpath to have a jsoup bag. |
Hibernate supplemental Annotations, the last 3 are not commonly used, can be ignored.
The difference between the @notnull @NotEmpty @NotBlank 3 annotations is mainly distinguished:
@NotNull value of any object cannot be null
The elements of the @NotEmpty collection object are not 0, that is, the collection is not empty, or it can be used for strings that are NOT null
@NotBlank can only be used for strings that are not null, and the string trim () after length is greater than 0
For an example of use:
[Java]View PlainCopy
- Public class User {
- @NotBlank
- private String name;
- //Older than 18 years
- @Min (+)
- private int age;
- @Email
- private String Email;
- //nested validation
- @Valid
- private Product Products;
- ... //Omit Getter,setter
- }
- Public class Product {
- @NotBlank
- private String name;
- //Price between 10 yuan-50 yuan
- @Range (min=10,max=)
- private int price;
- ... //Omit Getter,setter
- }
Hibernate Validator Annotation Daquan