Skip to content

Commit a698b1b

Browse files
committed
Refine validation section for controllers
Closes gh-35759
1 parent 2060c34 commit a698b1b

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

framework-docs/modules/ROOT/pages/web/webflux/controller/ann-validation.adoc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ Spring WebFlux has built-in xref:core/validation/validator.adoc[Validation] for
77
`@RequestMapping` methods, including xref:core/validation/beanvalidation.adoc[Java Bean Validation].
88
Validation may be applied at one of two levels:
99

10-
1. xref:web/webflux/controller/ann-methods/modelattrib-method-args.adoc[@ModelAttribute],
10+
1. Java Bean Validation is applied individually to an
11+
xref:web/webflux/controller/ann-methods/modelattrib-method-args.adoc[@ModelAttribute],
1112
xref:web/webflux/controller/ann-methods/requestbody.adoc[@RequestBody], and
12-
xref:web/webflux/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 `WebExchangeBindException`.
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`.
13+
xref:web/webflux/controller/ann-methods/multipart-forms.adoc[@RequestPart] method parameter
14+
annotated with `@jakarta.validation.Valid` or Spring's `@Validated` so long as
15+
it is a command object rather than a container such as `Map` or `Collection`, it does not
16+
have `Errors` or `BindingResult` immediately after in the method signature, and does not
17+
otherwise require method validation (see next). `WebExchangeBindException` is the
18+
exception raised when validating a method parameter individually.
19+
20+
2. Java Bean Validation is applied to the method when `@Constraint` annotations such as
21+
`@Min`, `@NotBlank` and others are declared directly on method parameters, or on the
22+
method for the return value, and it supersedes any validation that would be applied
23+
otherwise to a method parameter individually because method validation covers both
24+
method parameter constraints and nested constraints via `@Valid`.
25+
`HandlerMethodValidationException` is the exception raised validation is applied
26+
to the method.
2327

2428
Applications must handle both `WebExchangeBindException` and
2529
`HandlerMethodValidationException` as either may be raised depending on the controller

framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-validation.adoc

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ Spring MVC has built-in xref:core/validation/validator.adoc[validation] for
77
`@RequestMapping` methods, including xref:core/validation/beanvalidation.adoc[Java Bean Validation].
88
Validation may be applied at one of two levels:
99

10-
1. xref:web/webmvc/mvc-controller/ann-methods/modelattrib-method-args.adoc[@ModelAttribute],
10+
1. Java Bean Validation is applied individually to an
11+
xref:web/webmvc/mvc-controller/ann-methods/modelattrib-method-args.adoc[@ModelAttribute],
1112
xref:web/webmvc/mvc-controller/ann-methods/requestbody.adoc[@RequestBody], and
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
13+
xref:web/webmvc/mvc-controller/ann-methods/multipart-forms.adoc[@RequestPart] method parameter
14+
annotated with `@jakarta.validation.Valid` or Spring's `@Validated` so long as
15+
it is a command object rather than a container such as `Map` or `Collection`, it does not
16+
have `Errors` or `BindingResult` immediately after in the method signature, and does not
17+
otherwise require method validation (see next). `MethodArgumentNotValidException` is the
18+
exception raised when validating a method parameter individually.
19+
20+
2. Java Bean Validation is applied to the method when `@Constraint` annotations such as
21+
`@Min`, `@NotBlank` and others are declared directly on method parameters, or on the
22+
method for the return value, and it supersedes any validation that would be applied
23+
otherwise to a method parameter individually because method validation covers both
24+
method parameter constraints and nested constraints via `@Valid`.
25+
`HandlerMethodValidationException` is the exception raised validation is applied
26+
to the method.
27+
28+
Applications should handle both `MethodArgumentNotValidException` and
29+
`HandlerMethodValidationException` since either may be raised depending on the controller
2630
method signature. The two exceptions, however are designed to be very similar, and can be
2731
handled with almost identical code. The main difference is that the former is for a single
2832
object while the latter is for a list of method parameters.

0 commit comments

Comments
 (0)