Chapter 11. Bean Validation

Bean Validation is defined as JSR 303, and the complete specification can be downloaded from

Bean Validation provides a class-level constraint declaration and validation facility for Java applications.

The constraints can be declared in the form of annotations placed on a field, property, method parameter, or class. Constraints can be defined on interfaces or superclasses. Specifying a constraint on an interface ensures the constraint is enforced on classes implementing the interface. Similarly, all classes inheriting from a superclass inherit the validation behavior as well. Constraints declared on an interface or superclass are validated along with any constraints defined on the implementing or overriding class.

Validation constraints and configuration information can also be defined through XML validation descriptors in META-INF/validation.xml. The descriptors override and extend the meta-data defined using annotations. This chapter will cover annotations-based constraint validations only.

A constraint metadata repository and an ability to query it is also available. This is primarily targeted toward tool developers as well as integration with other frameworks and libraries.

Built-in Constraints

A built-in set of constraint definitions are available that can be used on beans. Multiple constraints can be specified on a bean to ensure different validation requirements are met. These constraints ...

Get Java EE 6 Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.