Chapter 11. Bean Validation

Bean Validation is defined as JSR 349, and the complete specification can be downloaded.

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 metadata defined via annotations. This chapter will cover annotation-based constraint validations only.

The specification also includes a constraint metadata repository and the capability to query it. This is primarily targeted toward tool development as well as integration with other frameworks and libraries.

Built-in Constraints

Bean Validation offers a built-in set of constraint definitions that can be used on beans. Multiple constraints can be specified on a bean to ensure different validation requirements are met. These constraints can also be used for composing other constraints. ...

Get Java EE 7 Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.