Skip to content

Commit ac11b03

Browse files
committed
Document customization of method validation errors
Closes gh-30653
1 parent df2617d commit ac11b03

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

framework-docs/modules/ROOT/pages/core/validation/beanvalidation.adoc

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ xref:core/aop/proxying.adoc#aop-understanding-aop-proxies[Understanding AOP Prox
329329
to always use methods and accessors on proxied classes; direct field access will not work.
330330
====
331331

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+
332342
By default, `jakarta.validation.ConstraintViolationException` is raised with the set of
333343
``ConstraintViolation``s returned by `jakarata.validation.Validator`. As an alternative,
334344
you can have `MethodValidationException` raised instead with ``ConstraintViolation``s
@@ -373,18 +383,86 @@ fields and properties, the `ParameterValidationResult` is `ParameterErrors` whic
373383
implements `org.springframework.validation.Errors` and exposes validation errors as
374384
``FieldError``s.
375385

386+
387+
[[validation-beanvalidation-spring-method-i18n]]
388+
==== Customizing Validation Errors
389+
376390
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.
380393

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:
386442

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+
----
387450

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+
----
388466

389467

390468
[[validation-beanvalidation-spring-other]]

framework-docs/modules/ROOT/pages/web/webflux/ann-rest-exceptions.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ Message codes and arguments for each error are also resolved via `MessageSource`
136136

137137
|===
138138

139+
NOTE: Unlike other exceptions, the message arguments for
140+
`WebExchangeBindException` and `HandlerMethodValidationException` are based on a list of
141+
`MessageSourceResolvable` errors that can also be customized through a
142+
xref:core/beans/context-introduction.adoc#context-functionality-messagesource[MessageSource]
143+
resource bundle. See
144+
xref:core/validation/beanvalidation.adoc#validation-beanvalidation-spring-method-i18n[Customizing Validation Errors]
145+
for more details.
146+
139147

140148

141149

framework-docs/modules/ROOT/pages/web/webmvc/mvc-ann-rest-exceptions.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ Message codes and arguments for each error are also resolved via `MessageSource`
180180

181181
|===
182182

183+
NOTE: Unlike other exceptions, the message arguments for
184+
`MethodArgumentValidException` and `HandlerMethodValidationException` are baed on a list of
185+
`MessageSourceResolvable` errors that can also be customized through a
186+
xref:core/beans/context-introduction.adoc#context-functionality-messagesource[MessageSource]
187+
resource bundle. See
188+
xref:core/validation/beanvalidation.adoc#validation-beanvalidation-spring-method-i18n[Customizing Validation Errors]
189+
for more details.
183190

184191

185192

0 commit comments

Comments
 (0)