|
3 | 3 |
|
4 | 4 | [.small]#xref:web/webflux/controller/ann-validation.adoc[See equivalent in the Reactive stack]#
|
5 | 5 |
|
6 |
| -Spring MVC has built-in xref:core/validation/validator.adoc[Validation] support for |
7 |
| -`@RequestMapping` methods, including the option to use |
8 |
| -xref:core/validation/beanvalidation.adoc[Java Bean Validation]. |
9 |
| -The validation support works on two levels. |
| 6 | +Spring MVC has built-in xref:core/validation/validator.adoc[validation] for |
| 7 | +`@RequestMapping` methods, including xref:core/validation/beanvalidation.adoc[Java Bean Validation]. |
| 8 | +Validation may be applied at one of two levels: |
10 | 9 |
|
11 |
| -First, resolvers for |
12 |
| -xref:web/webmvc/mvc-controller/ann-methods/modelattrib-method-args.adoc[@ModelAttribute], |
| 10 | +1. xref:web/webmvc/mvc-controller/ann-methods/modelattrib-method-args.adoc[@ModelAttribute], |
13 | 11 | xref:web/webmvc/mvc-controller/ann-methods/requestbody.adoc[@RequestBody], and
|
14 |
| -xref:web/webmvc/mvc-controller/ann-methods/multipart-forms.adoc[@RequestPart] method |
15 |
| -parameters perform validation if the parameter has Jakarta's `@Valid` or Spring's |
16 |
| -`@Validated` annotation, and raise `MethodArgumentNotValidException` if necessary. |
17 |
| -Alternatively, you can handle the errors in the controller method by adding an |
18 |
| -`Errors` or `BindingResult` method parameter immediately after the validated one. |
19 |
| - |
20 |
| -Second, if {bean-validation-site}[Java Bean Validation] is present _AND_ any method |
21 |
| -parameter has `@Constraint` annotations, then method validation is applied instead, |
22 |
| -raising `HandlerMethodValidationException` if necessary. For this case you can still add |
23 |
| -an `Errors` or `BindingResult` method parameter to handle validation errors within the |
24 |
| -controller method, but if other method arguments have validation errors then |
25 |
| -`HandlerMethodValidationException` is raised instead. Method validation can apply |
26 |
| -to the return value if the method is annotated with `@Valid` or with `@Constraint` |
27 |
| -annotations. |
| 12 | +xref:web/webmvc/mvc-controller/ann-methods/multipart-forms.adoc[@RequestPart] argument |
| 13 | +resolvers validate a method argument individually if the method parameter is annotated |
| 14 | +with Jakarta `@Valid` or Spring's `@Validated`, _AND_ there is no `Errors` or |
| 15 | +`BindingResult` parameter immediately after, _AND_ method validation is not needed (to be |
| 16 | +discussed next). The exception raised in this case is `MethodArgumentNotValidException`. |
| 17 | + |
| 18 | +2. When `@Constraint` annotations such as `@Min`, `@NotBlank` and others are declared |
| 19 | +directly on method parameters, or on the method (for the return value), then method |
| 20 | +validation must be applied, and that supersedes validation at the method argument level |
| 21 | +because method validation covers both method parameter constraints and nested constraints |
| 22 | +via `@Valid`. The exception raised in this case is `HandlerMethodValidationException`. |
| 23 | + |
| 24 | +Applications must handle both `MethodArgumentNotValidException` and |
| 25 | +`HandlerMethodValidationException` as either may be raised depending on the controller |
| 26 | +method signature. The two exceptions, however are designed to be very similar, and can be |
| 27 | +handled with almost identical code. The main difference is that the former is for a single |
| 28 | +object while the latter is for a list of method parameters. |
| 29 | + |
| 30 | +NOTE: `@Valid` is not a constraint annotation, but rather for nested constraints within |
| 31 | +an Object. Therefore, by itself `@Valid` does not lead to method validation. `@NotNull` |
| 32 | +on the other hand is a constraint, and adding it to an `@Valid` parameter leads to method |
| 33 | +validation. For nullability specifically, you may also use the `required` flag of |
| 34 | +`@RequestBody` or `@ModelAttribute`. |
| 35 | + |
| 36 | +Method validation may be used in combination with `Errors` or `BindingResult` method |
| 37 | +parameters. However, the controller method is called only if all validation errors are on |
| 38 | +method parameters with an `Errors` immediately after. If there are validation errors on |
| 39 | +any other method parameter then `HandlerMethodValidationException` is raised. |
28 | 40 |
|
29 | 41 | You can configure a `Validator` globally through the
|
30 | 42 | xref:web/webmvc/mvc-config/validation.adoc[WebMvc config], or locally through an
|
|
0 commit comments