@@ -123,15 +123,12 @@ Validator, is expected to be present in the classpath and is automatically detec
123
123
124
124
125
125
[[validation-beanvalidation-spring-inject]]
126
- === Injecting a Validator
126
+ === Inject Jakarta Validator
127
127
128
128
`LocalValidatorFactoryBean` implements both `jakarta.validation.ValidatorFactory` and
129
- `jakarta.validation.Validator`, as well as Spring's `org.springframework.validation.Validator`.
130
- You can inject a reference to either of these interfaces into beans that need to invoke
131
- validation logic.
132
-
133
- You can inject a reference to `jakarta.validation.Validator` if you prefer to work with the Bean
134
- Validation API directly, as the following example shows:
129
+ `jakarta.validation.Validator`, so you can inject a reference to the latter to
130
+ apply validation logic if you prefer to work with the Bean Validation API directly,
131
+ as the following example shows:
135
132
136
133
[tabs]
137
134
======
@@ -160,8 +157,15 @@ Kotlin::
160
157
----
161
158
======
162
159
163
- You can inject a reference to `org.springframework.validation.Validator` if your bean
164
- requires the Spring Validation API, as the following example shows:
160
+
161
+ [[validation-beanvalidation-spring-inject-adapter]]
162
+ === Inject Spring Validator
163
+
164
+ In addition to implementing `jakarta.validation.Validator`, `LocalValidatorFactoryBean`
165
+ also adapts to `org.springframework.validation.Validator`, so you can inject a reference
166
+ to the latter if your bean requires the Spring Validation API.
167
+
168
+ For example:
165
169
166
170
[tabs]
167
171
======
@@ -190,9 +194,15 @@ Kotlin::
190
194
----
191
195
======
192
196
197
+ When used as `org.springframework.validation.Validator`, `LocalValidatorFactoryBean`
198
+ invokes the underlying `jakarta.validation.Validator`, and then adapts
199
+ ``ContraintViolation``s to ``FieldError``s, and registers them with the `Errors` object
200
+ passed into the `validate` method.
201
+
202
+
193
203
194
204
[[validation-beanvalidation-spring-constraints]]
195
- === Configuring Custom Constraints
205
+ === Configure Custom Constraints
196
206
197
207
Each bean validation constraint consists of two parts:
198
208
@@ -274,9 +284,8 @@ As the preceding example shows, a `ConstraintValidator` implementation can have
274
284
[[validation-beanvalidation-spring-method]]
275
285
=== Spring-driven Method Validation
276
286
277
- You can integrate the method validation feature supported by Bean Validation 1.1 (and, as
278
- a custom extension, also by Hibernate Validator 4.3) into a Spring context through a
279
- `MethodValidationPostProcessor` bean definition:
287
+ You can integrate the method validation feature of Bean Validation into a
288
+ Spring context through a `MethodValidationPostProcessor` bean definition:
280
289
281
290
[tabs]
282
291
======
@@ -305,11 +314,11 @@ XML::
305
314
----
306
315
======
307
316
308
- To be eligible for Spring-driven method validation, all target classes need to be annotated
317
+ To be eligible for Spring-driven method validation, target classes need to be annotated
309
318
with Spring's `@Validated` annotation, which can optionally also declare the validation
310
319
groups to use. See
311
320
{api-spring-framework}/validation/beanvalidation/MethodValidationPostProcessor.html[`MethodValidationPostProcessor`]
312
- for setup details with the Hibernate Validator and Bean Validation 1.1 providers.
321
+ for setup details with the Hibernate Validator and Bean Validation providers.
313
322
314
323
[TIP]
315
324
====
@@ -320,6 +329,61 @@ xref:core/aop/proxying.adoc#aop-understanding-aop-proxies[Understanding AOP Prox
320
329
to always use methods and accessors on proxied classes; direct field access will not work.
321
330
====
322
331
332
+ By default, `jakarta.validation.ConstraintViolationException` is raised with the set of
333
+ ``ConstraintViolation``s returned by `jakarata.validation.Validator`. As an alternative,
334
+ you can have `MethodValidationException` raised instead with ``ConstraintViolation``s
335
+ adapted to `MessageSourceResolvable` errors. To enable set the following flag:
336
+
337
+ [tabs]
338
+ ======
339
+ Java::
340
+ +
341
+ [source,java,indent=0,subs="verbatim,quotes",role="primary"]
342
+ ----
343
+ import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
344
+
345
+ @Configuration
346
+ public class AppConfig {
347
+
348
+ @Bean
349
+ public MethodValidationPostProcessor validationPostProcessor() {
350
+ MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
351
+ processor.setAdaptConstraintViolations(true);
352
+ return processor;
353
+ }
354
+ }
355
+
356
+ ----
357
+
358
+ XML::
359
+ +
360
+ [source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
361
+ ----
362
+ <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
363
+ <property name="adaptConstraintViolations" value="true"/>
364
+ </bean>
365
+ ----
366
+ ======
367
+
368
+ `MethodValidationException` contains a list of ``ParameterValidationResult``s which
369
+ group errors by method parameter, and each exposes a `MethodParameter`, the argument
370
+ value, and a list of `MessageSourceResolvable` errors adapted from
371
+ ``ConstraintViolation``s. For `@Valid` method parameters with cascaded violations on
372
+ fields and properties, the `ParameterValidationResult` is `ParameterErrors` which
373
+ implements `org.springframework.validation.Errors` and exposes validation errors as
374
+ ``FieldError``s.
375
+
376
+ The adapted `MessageSourceResolvable` errors can be turned into error messages to
377
+ display to users through the configured
378
+ xref:core/beans/context-introduction.adoc#context-functionality-messagesource[`MessageSource`]
379
+ based on locale and language specific resource bundles.
380
+
381
+ NOTE: Spring MVC and WebFlux have built-in support for method validation, and therefore
382
+ for web controller methods there is no need for a class level `@Validated` and an AOP proxy.
383
+ See the Spring MVC xref:web/webmvc/mvc-controller/ann-validation.adoc[Validation] section,
384
+ the WebFlux xref:web/webflux/controller/ann-validation.adoc[Validation] section,
385
+ and the xref:web/webmvc/mvc-controller/ann-validation.adoc[Error Responses] section.
386
+
323
387
324
388
325
389
0 commit comments