@@ -329,6 +329,16 @@ xref:core/aop/proxying.adoc#aop-understanding-aop-proxies[Understanding AOP Prox
329
329
to always use methods and accessors on proxied classes; direct field access will not work.
330
330
====
331
331
332
+ NOTE: Spring MVC and WebFlux have built-in support for method validation, and therefore
333
+ for web controller methods there is no need for a class level `@Validated` and an AOP proxy.
334
+ See the Spring MVC xref:web/webmvc/mvc-controller/ann-validation.adoc[Validation] section,
335
+ the WebFlux xref:web/webflux/controller/ann-validation.adoc[Validation] section,
336
+ and the xref:web/webmvc/mvc-controller/ann-validation.adoc[Error Responses] section.
337
+
338
+
339
+ [[validation-beanvalidation-spring-method-exceptions]]
340
+ ==== Method Validation Exceptions
341
+
332
342
By default, `jakarta.validation.ConstraintViolationException` is raised with the set of
333
343
``ConstraintViolation``s returned by `jakarata.validation.Validator`. As an alternative,
334
344
you can have `MethodValidationException` raised instead with ``ConstraintViolation``s
@@ -373,18 +383,86 @@ fields and properties, the `ParameterValidationResult` is `ParameterErrors` whic
373
383
implements `org.springframework.validation.Errors` and exposes validation errors as
374
384
``FieldError``s.
375
385
386
+
387
+ [[validation-beanvalidation-spring-method-i18n]]
388
+ ==== Customizing Validation Errors
389
+
376
390
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.
391
+ display to users through the configured `MessageSource` with locale and language specific
392
+ resource bundles. This section provides an example for illustration.
380
393
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.
394
+ Given the following class declarations:
395
+
396
+ [tabs]
397
+ ======
398
+ Java::
399
+ +
400
+ [source,java,indent=0,subs="verbatim,quotes",role="primary"]
401
+ ----
402
+ record Person(@Size(min = 1, max = 10) String name) {
403
+ }
404
+
405
+ @Validated
406
+ public class MyService {
407
+
408
+ void addStudent(@Valid Person person, @Max(2) int degrees) {
409
+ // ...
410
+ }
411
+ }
412
+ ----
413
+
414
+ Kotlin::
415
+ +
416
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
417
+ ----
418
+ @JvmRecord
419
+ internal data class Person(@Size(min = 1, max = 10) val name: String)
420
+
421
+ @Validated
422
+ class MyService {
423
+
424
+ fun addStudent(person: @Valid Person?, degrees: @Max(2) Int) {
425
+ // ...
426
+ }
427
+ }
428
+ ----
429
+ ======
430
+
431
+ A `ConstraintViolation` on `Person.name()` is adapted to a `FieldErrro` with the following:
432
+
433
+ - Error codes `"Size.student.name"`, `"Size.name"`, `"Size.java.lang.String"`, and `"Size"`
434
+ - Message arguments `"name"`, `10`, and `1` (the field name and the constraint attributes)
435
+ - Default message "size must be between 1 and 10"
436
+
437
+ To customize the default message, you can add properties to
438
+ xref:core/beans/context-introduction.adoc#context-functionality-messagesource[MessageSource]
439
+ resource bundles using any of the above errors codes and message arguments. Note also that the
440
+ message argument `"name"` is itself a `MessagreSourceResolvable` with error codes
441
+ `"student.name"` and `"name"` and can customized too. For example:
386
442
443
+ Properties::
444
+ +
445
+ [source,properties,indent=0,subs="verbatim,quotes",role="secondary"]
446
+ ----
447
+ Size.student.name=Please, provide a {0} that is between {2} and {1} characters long
448
+ student.name=username
449
+ ----
387
450
451
+ A `ConstraintViolation` on the `degrees` method parameter is adapted to a
452
+ `MessageSourceResolvable` with the following:
453
+
454
+ - Error codes `"Max.myService#addStudent.degrees"`, `"Max.degrees"`, `"Max.int"`, `"Max"`
455
+ - Message arguments "degrees2 and 2 (the field name and the constraint attribute)
456
+ - Default message "must be less than or equal to 2"
457
+
458
+ To customize the above default message, you can add a property such as:
459
+
460
+ Properties::
461
+ +
462
+ [source,properties,indent=0,subs="verbatim,quotes",role="secondary"]
463
+ ----
464
+ Max.degrees=You cannot provide more than {1} {0}
465
+ ----
388
466
389
467
390
468
[[validation-beanvalidation-spring-other]]
0 commit comments