Null Safety
Although Java does not allow expressing null-safety with its type system, Reactor now provides annotations to declare nullability of APIs, similar to those provided by Spring Framework 5.
Reactor uses these annotations, but they can also be used in any Reactor-based Java project to declare null-safe APIs. Nullability of the types used inside method bodies is outside of the scope of this feature.
These annotations are meta-annotated with JSR 305
annotations (a dormant JSR that is supported by tools such as IntelliJ IDEA) to provide
useful warnings to Java developers related to null-safety in order to avoid
NullPointerException
at runtime. JSR 305 meta-annotations let tooling vendors
provide null safety support in a generic way, without having to hard-code support for Reactor annotations.
It is not necessary nor recommended with Kotlin 1.1.5+ to have a dependency on JSR 305 in your project classpath. |
They are also used by Kotlin, which natively supports null safety. See this dedicated section for more details.
The following annotations are provided in the reactor.util.annotation
package:
-
@NonNull
: Indicates that a specific parameter, return value, or field cannot benull
. (It is not needed on parameters and return values where@NonNullApi
applies) . -
@Nullable
: Indicates that a parameter, return value, or field can benull
. -
@NonNullApi
: Package-level annotation that indicates non-null is the default behavior for parameters and return values.
Nullability for generic type arguments, variable arguments, and array elements is not yet supported. See issue #878 for up-to-date information. |