Skip to content

Commit be5eb70

Browse files
committed
Explicit notes on non-null enforcement and deep cause support in 5.3
Closes gh-26296 See gh-26317
1 parent fff2291 commit be5eb70

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/ExceptionHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,9 @@
3333
* <li>An exception argument: declared as a general Exception or as a more
3434
* specific exception. This also serves as a mapping hint if the annotation
3535
* itself does not narrow the exception types through its {@link #value()}.
36+
* You may refer to a top-level exception being propagated or to a nested
37+
* cause within a wrapper exception. As of 5.3, any cause level is being
38+
* exposed, whereas previously only an immediate cause was considered.
3639
* <li>Request and/or response objects (typically from the Servlet API).
3740
* You may choose any specific request/response type, e.g.
3841
* {@link javax.servlet.ServletRequest} / {@link javax.servlet.http.HttpServletRequest}.

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,17 @@ This can be the case for `Long`, `UUID`, and other target types. If you want to
22222222
to be injected, either use the `required` flag on the argument annotation, or declare the
22232223
argument as `@Nullable`.
22242224

2225+
[NOTE]
2226+
====
2227+
As of 5.3, non-null arguments will be enforced even after type conversion. If your handler
2228+
method intends to accept a null value as well, either declare your argument as `@Nullable`
2229+
or mark it as `required=false` in the corresponding `@RequestParam` etc annotation. This is
2230+
a best practice and the recommended solution for regressions encountered in a 5.3 upgrade.
2231+
2232+
Alternatively, you may specifically handle e.g. the resulting `MissingPathVariableException`
2233+
in the case of a required `@PathVariable`. A null value after conversion will be treated like
2234+
an empty original value, so the corresponding `Missing...Exception` variants will be thrown.
2235+
====
22252236

22262237

22272238
[[mvc-ann-matrix-variables]]
@@ -3683,14 +3694,15 @@ controller-specific `Formatter` implementations, as the following example shows:
36833694
}
36843695
----
36853696

3686-
The exception may match against a top-level exception being propagated (that is, a direct
3687-
`IOException` being thrown) or against the immediate cause within a top-level wrapper exception
3688-
(for example, an `IOException` wrapped inside an `IllegalStateException`).
3697+
The exception may match against a top-level exception being propagated (e.g. a direct
3698+
`IOException` being thrown) or against a nested cause within a wrapper exception (e.g.
3699+
an `IOException` wrapped inside an `IllegalStateException`). As of 5.3, this can match
3700+
at arbitrary cause levels, whereas previously only an immediate cause was considered.
36893701

36903702
For matching exception types, preferably declare the target exception as a method argument,
3691-
as the preceding example shows. When multiple exception methods match, a root exception match is generally
3692-
preferred to a cause exception match. More specifically, the `ExceptionDepthComparator` is
3693-
used to sort exceptions based on their depth from the thrown exception type.
3703+
as the preceding example shows. When multiple exception methods match, a root exception match is
3704+
generally preferred to a cause exception match. More specifically, the `ExceptionDepthComparator`
3705+
is used to sort exceptions based on their depth from the thrown exception type.
36943706

36953707
Alternatively, the annotation declaration may narrow the exception types to match,
36963708
as the following example shows:

0 commit comments

Comments
 (0)