Route Compile Time Validation

Micronaut supports validating route arguments at compile time with the validation library. To get started simply add the validation dependency to your build:

build.gradle

  1. annotationProcessor "io.micronaut:micronaut-validation" // Java only
  2. kapt "io.micronaut:micronaut-validation" // Kotlin only
  3. compile "io.micronaut:micronaut-validation"

With the correct dependency on your classpath, route arguments will automatically be checked at compile time. The 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 an java.util.Optional.

An optional variable is one that will allow the route to match a URI even if the value is not present. For example /foo{/bar} will match 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.