Bean Validation 1.1 in Java EE 7 – Using Default Constraints

Bean Validation 1.1 (JSR 349) is integrated as part of Java EE 7. The new features of Bean Validation 1.1 are as follows:

Applying constraints on method parameters and return values. This can be used in creating a pre and post condition contract.

Constraints can be applied to constructors.

New API to obtain metadata of constraint and constraint objects.

What is Bean Validation?

Bean validation is an attempt to provide constructs for defining constraints for your data i.e Java beans by using inbuilt i.e default constraint rules and also by creating custom constraint rules. These constraints can be used to validate beans, attributes, constructors, method return types and parameters. There are numerous reference implementations available and Hibernate Validator provides support for the latest Bean Validation 1.1 specification.

In this post I will create a Java bean representing Book details like ISBN number, Title, Publication, Authors, Edition, Publication Date, Number of Pages, Cost and define constraints on these attributes using the default constraint rules provided by the Bean Validation reference implementation. The default constraints available are:

Constraint

Description

AssertFalse AssertTrue

The element should either be true or false.

DecimalMin DecimalMax

The element should have a value greater than or lower than the specified value.

Future Past

The date has to be in future The date has to be in past.

Max, Min

The specified value has to be greater than or lower than the specified value.

Null, NotNull

The specified element must be null or not null.

Pattern

The specified element’s value should match the given pattern.

Digits

The specified element’s value must has value within the accepted range.

Size

The specified element’s value’s size must be between the specified values.

And to see these constraints in action I have created a JUnit test which creates a Book instance and then updates the attributes with values which violate the constraint and then run the validator against the Book instance to identify and print the violations captured.

*********Authors Validation tests***************
The following violations found:
Violation: may not be null
The following violations found:
Violation: size must be between 1 and 2147483647
*********ISBN Validation tests***************
The following violations found:
Violation: may not be null
The following violations found:
Violation: size must be between 10 and 10
*********No Validations Violation tests**************
No violations found!!!
*********Title Validation tests***************
The following violations found:
Violation: may not be null
The following violations found:
Violation: size must be between 10 and 2147483647
*********Page Count Validation tests***************
The following violations found:
Violation: must be greater than or equal to 50
*********Edition Validation tests***************
The following violations found:
Violation: must be greater than or equal to 1
*********Publisher Validation tests***************
The following violations found:
Violation: may not be null
The following violations found:
Violation: size must be between 5 and 2147483647
*********Cost Validation tests***************
The following violations found:
Violation: may not be null
*********Publication Date Validation tests***************
The following violations found:
Violation: may not be null

As it is pretty straight forward to make use of the default constraints provided by Bean Validation API, I haven’t gone into detail on explaining the same. In the future post I will write about how one can create own constraints and use them as annotation to validate your business logic.