Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java EE - JSR 303 Bean validation - Pierre-Johan CHARTRE

Similar presentations


Presentation on theme: "Java EE - JSR 303 Bean validation - Pierre-Johan CHARTRE"— Presentation transcript:

1 Java EE - JSR 303 Bean validation - Pierre-Johan CHARTRE pierre-johan.chartre@logica.com

2 Goals “Validating data is a common task that is spread across several (if not all) layers of today's applications.” The JSR-303 Bean Validation specification addresses this problem by providing: an extensible annotation-based validation declaration model (XML deployment descriptors will be available as well) a standard runtime validation API a standard metadata query API

3 Expressing constraints Annotation-based or XML-based definition on class, field, or getter Validating an object is equivalent to validate all the constraints declared on: its fields –its getters –its class –its superclasses –its interfaces

4 Use constraints Message A constraint can define a custom error message returned when failure occurs. Object graph validation It is handy to validate an object graph as a whole rather than validate every single object manually. @Valid allows you to include associated objects to the validation process

5 The Validator API A Validator validates objects (graphs) of a given type. When an object is validated, all declared constraints are validated (from fields, getters, the class itself and associated objects if marked as such). The validation will process all constraints and return the list of all failing ones. This is quite useful to display all erroneous fields in a user interface. Object: Validate an object Value: Validate a value Constraint groups: Validate a subset of the declared constraints rather than all available constaints (using the notion of constraint groups) validate a gradually populated object graph (wizard style UI or use case driven validation) validate a set of constraints before an other set of constraints (the second relying on the first or being expensive resource-wise)

6 Mind your own constraint Constraints are not limited to built-in annotations. Any application can define its own additional set of constraints. A constraint is comprised of: an annotation a constraint validation implementation

7 Write a custom constraint As simple API to create a constraint Create a new annotation (@interface) With runtime retention policy Annotated with @ConstraintValidator they must have a message attribute they must have a groups attribute Usage:

8 Write a custom constraint The validator implementation The constraint implementation is associated to its annotation through the use of @ConstraintValidator. The implementation must implement a very simple interface Constraint where A is the targeted constraint annotation

9 Write a custom constraint

10 Other feature Class level constraints Same approach ! Constraints group Constraints ordering

11 Conclusion Standard API Declarative model Flexible API Stay as simple and understandable as possible Plug and play API with JSF: EL value binding validation JPA: before create/merge

12 Keywords Bean validation @Valid @ConstraintValidator @Size @Length @Max @NotNull Constraint


Download ppt "Java EE - JSR 303 Bean validation - Pierre-Johan CHARTRE"

Similar presentations


Ads by Google