Hibernate Validator
JSR 349 Reference
Implementation
使用指南
5.0.1.Final
由 Hardy Ferentschik和Gunnar Morling
and thanks to Shaozhuang Liu
iii
序言 ........................................................................... vii
1. Getting started .............................................................. 1
1.1. Project set up ........................................................ 1
1.1.1. Unified EL ..................................................... 1
1.1.2. CDI ............................................................. 2
1.2. Applying constraints .................................................. 2
1.3. Validating constraints ................................................ 3
1.4. Where to go next? ..................................................... 5
2. Declaring and validating bean constraints .................................... 7
2.1. Declaring bean constraints ............................................ 7
2.1.1. Field-level constraints ......................................... 7
2.1.2. Property-level constraints ...................................... 8
2.1.3. Class-level constraints ......................................... 9
2.1.4. Constraint inheritance ......................................... 10
2.1.5. Object graphs .................................................. 11
2.2. Validating bean constraints .......................................... 12
2.2.1. Obtaining a Validator instance ................................. 12
2.2.2. Validator methods .............................................. 13
2.2.3. ConstraintViolation methods .................................... 14
2.3. Built-in constraints ................................................. 15
2.3.1. Bean Validation constraints .................................... 15
2.3.2. Additional constraints ......................................... 19
3. Declaring and validating method constraints ................................. 25
3.1. Declaring method constraints ......................................... 26
3.1.1. Parameter constraints .......................................... 26
3.1.2. Return value constraints ....................................... 28
3.1.3. Cascaded validation ............................................ 29
3.1.4. Method constraints in inheritance hierarchies .................. 31
3.2. Validating method constraints ........................................ 33
3.2.1. Obtaining an ExecutableValidator instance ...................... 34
3.2.2. ExecutableValidator methods .................................... 34
3.2.3. ConstraintViolation methods for method validation .............. 37
4. Interpolating constraint error messages ..................................... 39
4.1. Default message interpolation ........................................ 39
4.1.1. Special characters ............................................. 40
4.1.2. Interpolation with message expressions ......................... 40
4.1.3. Examples ....................................................... 41
4.2. Custom message interpolation ......................................... 43
4.2.1. ResourceBundleLocator .......................................... 43
5. Grouping constraints ........................................................ 45
5.1. Requesting groups .................................................... 45
5.2. Defining group sequences ............................................. 49
5.3. Redefining the default group sequence ................................ 50
5.3.1. @GroupSequence ................................................. 50
Hibernate Validator
iv
5.3.2. @GroupSequenceProvider ......................................... 51
5.4. Group conversion ..................................................... 52
6. Creating custom constraints ................................................. 57
6.1. Creating a simple constraint ......................................... 57
6.1.1. The constraint annotation ...................................... 57
6.1.2. The constraint validator ....................................... 59
6.1.3. The error message ............................................. 62
6.1.4. Using the constraint .......................................... 62
6.2. Class-level constraints ............................................... 63
6.2.1. Custom property paths ......................................... 64
6.3. Cross-parameter constraints ........................................... 65
6.4. Constraint composition ............................................... 68
7. Configuring via XML ......................................................... 71
7.1. Configuring the validator factory in validation.xml ................... 71
7.2. Mapping constraints via constraint-mappings ........................... 74
8. Bootstrapping ............................................................... 81
8.1. Retrieving ValidatorFactory and Validator ............................. 81
8.1.1. ValidationProviderResolver ..................................... 82
8.2. Configuring a ValidatorFactory ....................................... 83
8.2.1. MessageInterpolator ............................................ 83
8.2.2. TraversableResolver ............................................ 84
8.2.3. ConstraintValidatorFactory ..................................... 85
8.2.4. ParameterNameProvider .......................................... 86
8.2.5. Adding mapping streams ......................................... 87
8.2.6. Provider-specific settings ..................................... 87
8.3. Configuring a Validator .............................................. 88
9. Using constraint metadata ................................................... 89
9.1. BeanDescriptor ........................................................ 90
9.2. PropertyDescriptor .................................................... 93
9.3. MethodDescriptor and ConstructorDescriptor ............................ 93
9.4. ElementDescriptor ..................................................... 95
9.5. GroupConversionDescriptor ............................................. 98
9.6. ConstraintDescriptor .................................................. 98
10. Integrating with other frameworks ......................................... 101
10.1. ORM集成 ............................................................. 101
10.1.1. 与数据库集成校验 ............................................. 101
10.1.2. 基于Hibernate事件模型的校验 .................................. 101
10.1.3. JPA .......................................................... 103
10.2. JSF & Seam ......................................................... 103
10.3. CDI ................................................................ 104
10.3.1. Dependency injection ......................................... 104
10.3.2. Method validation ............................................ 106
10.4. Java EE ............................................................ 110
11. Hibernate Validator Specifics ............................................. 111
v
11.1. Public API ......................................................... 111
11.2. Fail fast mode ..................................................... 112
11.3. Programmatic constraint declaration ................................. 113
11.4. Boolean composition of constraints ................................. 116
11.5. ResourceBundleLocator ............................................... 117
12. Annotation Processor ...................................................... 119
12.1. Prerequisites ....................................................... 119
12.2. Features ............................................................ 119
12.3. Options ............................................................. 120
12.4. Using the Annotation Processor ..................................... 120
12.4.1. Command line builds ......................................... 120
12.4.2. IDE builds ................................................... 122
12.5. Known issues ....................................................... 125
13. Further reading ........................................................... 127