Hibernate Validator Annotation Daquan

Source: Internet
Author: User

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
    1. Public class User {
    2. @NotBlank
    3. private String name;
    4. //Older than 18 years
    5. @Min (+)
    6. private int age;
    7. @Email
    8. private String Email;
    9. //nested validation
    10. @Valid
    11. private Product Products;
    12. ... //Omit Getter,setter
    13. }
    14. Public class Product {
    15. @NotBlank
    16. private String name;
    17. //Price between 10 yuan-50 yuan
    18. @Range (min=10,max=)
    19. private int price;
    20. ... //Omit Getter,setter
    21. }

Hibernate Validator Annotation Daquan

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.