By default, dropwizard allows you to use validation annotations on rest services. This allows you to use validation annotations the same way on any guice bean method.
Bundle is actually a wrapper for guice-validator project.
Maven:
<dependency>
<groupId>ru.vyarus.guicey</groupId>
<artifactId>guicey-validation</artifactId>
<version>{guicey.version}</version>
</dependency>
Gradle:
implementation 'ru.vyarus.guicey:guicey-validation:{guicey.version}'
Omit version if guicey BOM used.
By default, no setup required: bundle will be loaded automatically with the bundles lookup mechanism (enabled by default). So just add jar into classpath and annotations will work.
For example:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import ru.vyarus.guicey.annotations.lifecycle.PostStartup;
public class SampleBean {
private void doSomething(@NotNull String param) {
}
}
Call bean.doSomething(null)
will fail with ConstraintValidationException
.
For more usage examples see guice-validator documentation
By default, validations work in implicit mode: any method containing validation annotations would trigger validation on call.
If you want more explicitly mark methods requiring validation then register bundle manually:
.bundles(new ValidationBundle()
.validateAnnotatedOnly())
Now, only methods annotated with @ValidateOnExecution
(or all methods in annotated class)
will trigger validation.
If you want, you can use your own annotation:
.bundles(new ValidationBundle()
.validateAnnotatedOnly(MyAnnotation.class))
By default, validation is not applied to resource classes (annotated with @Path
) because
dropwizard already performs validation there. And rest methods, annotated with @GET
, @POST
, etc.
are skipped (required for complex declaration cases, like dynamic resource mappings or sub resources).
You can reduce this scope even further:
.bundles(new ValidationBundle()
.targetClasses(Matchers.subclassesOf(SomeService.class)
.and(Matchers.not(Matchers.annotatedWith(Path.class)))))
Here SomeService
is excluded from validation (its methods would not trigger validation).
Note that default condition (not resource) is appended.
Or excluding methods:
.bundles(new ValidationBundle()
.targetMethods(Matchers.annotatedWith(SuppressValidation.class)
.and(new DirectMethodMatcher())))
Now methods annotated with @SuppressValidation
will not be validated. Note that
.and(new DirectMethodMatcher())
condition was added to aslo exclude synthetic and bridge methods (jvm generated methods).
NOTE: you can verify AOP appliance with guicey .printGuiceAopMap()
report.
By default, Default
validation group is always enabled allowing you to not specify
groups for each call.
This could be disabled with bundle option:
.bundles(new ValidationBundle().strictGroupsDeclaration())
Read more in guice-validator docs.