Route Compile-Time Validation
Micronaut supports validating route arguments at compile time with the validation library. To get started, add the validation
dependency to your build:
build.gradle
annotationProcessor "io.micronaut:micronaut-validation" // Java only
kapt "io.micronaut:micronaut-validation" // Kotlin only
implementation "io.micronaut:micronaut-validation"
With the correct dependency on your classpath, route arguments will automatically be checked at compile time. Compilation will fail if any of the following conditions are met:
- The URI template contains a variable that is optional, but the method parameter is not annotated with
@Nullable
or is anjava.util.Optional
.
An optional variable is one that allows the route to match a URI even if the value is not present. For example /foo{/bar}
matches requests to /foo
and /foo/abc
. The non-optional variant would be /foo/{bar}
. See the URI Path Variables section for more information.
- The URI template contains a variable that is missing from the method arguments.
To disable route compile-time validation, set the system property -Dmicronaut.route.validation=false . For Java and Kotlin users using Gradle, the same effect can be achieved by removing the validation dependency from the annotationProcessor /kapt scope. |