The Bean Validation framework allows you to specify the validation rules in the form of annotations on a field, method, or a Java class. You can either use built-in constraints offered by Bean Validation or build custom constraints by using the extension mechanisms offered by the framework. The following table summarizes the built-in constraints offered by Bean Validation that are applicable to all data types:
Validation constraints |
Supported Java types |
Details of the imposed constraints |
@NotNull |
Applicable to all Java types |
The annotated variable must not be null. |
@Null |
Applicable to all Java types |
The annotated variable must be null. |
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the Boolean data types:
Validation constraints |
Supported Java types |
Details of the imposed constraints |
@AssertFalse |
|
The annotated variable must be false. |
@AssertTrue |
|
The annotated variable must be true. |
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the number data types:
Validation constraints |
Supported Java types |
Details of the imposed constraints |
@DecimalMax |
|
The annotated variable must not exceed the maximum specified limit. |
@DecimalMin |
|
The annotated variable must be higher than or equal to the minimum specified value. |
@Digits |
|
The annotated variable must be a number within the acceptable range. |
@Max |
|
The annotated variable must be a number with a value less than or equal to the specified maximum value. |
@Min |
|
The annotated variable must be a number with a value larger than or equal to the specified minimum value. |
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the date data types:
Validation constraints |
Supported Java types |
Details of the imposed constraints |
@Future |
|
The annotated variable must be a date in the future. |
@Past |
|
The annotated variable must be a date in the past. |
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the string data types:
Validation constraints |
Supported Java types |
Details of the imposed constraints |
@Pattern |
java.lang.String |
The annotated string must match the regular expression specified. |
The following table summarizes the built-in constraints offered by Bean Validation that are applicable to the collection and string data types:
Validation constraints |
Supported Java types |
Details of the imposed constraints |
@Size |
|
The annotated string or collection size must be between the specified boundaries (included). |
The following code snippet illustrates the use of built-in validation constraints provided by Bean Validation for validating an input to a JAX-RS method call. This example validates the input parameter for a positive number:
@GET @Path("departments/{id}") @Produces(MediaType.APPLICATION_JSON) public Department findDepartment(@PathParam("id") @Min(value = 0, message = "Department Id must be a positive value") Short id, @Context Request request) { Department department = findDepartmentEntity(id); return department; }