The purpose of this API is to provide a facility for the Programming by Contract approach for systems design based on the concepts defined by the Bean Validation API. More specifically this means that any Bean Validation constraints (built-in as well as custom constraints) can be used to describe
These constraints can relate to the parameters/return values themselves but it is also possible to trigger a recursive validation of these values using the special {@link Valid} annotation. If for instance considering the methoddeclaration
@NotNull @Valid Customer findCustomerByName(@NotNull @Size(min = 5) String name) { // ... }
the following conditions would hold (provided the method validation is triggered automatically by some integration layer, see below):
This service only copes with the actual validation of method parameters/return values itself, but not with the invocation of such a validation. It is expected that this invocation is triggered by an integration layer using AOP or similar method interception facilities such as JDK's {@link Proxy} API or
Such an integration layer would typically intercept each method call to be validated, validate the call's parameters, proceed with the method invocation and finally validate the invocation's return value. If any of the validation steps yields a non-empty set of constraint violations the integration layer would typically throw a {@link MethodConstraintViolationException} wrappingthese violations which in turn guarantees that the call flow only arrives at the method's implementation respectively call site if all pre- respectively postconditions are fulfilled.
MethodValidator
instances are obtained by {@link Validator#unwrap(Class) unwrapping} a {@link Validator} object:
Validator validator = ...; MethodValidator methodValidator = validator.unwrap(MethodValidator.class);Method level validation is (currently) a proprietary feature of Hibernate Validator (HV), so the unwrapped
Validator
instance must be HV's implementation. In case there are multiple Bean Validation implementations on the classpath, this can be done be explicitly choosing HV as validation provider: MethodValidator methodValidator = Validation.byProvider(HibernateValidator.class) .configure() .buildValidatorFactory() .getValidator() .unwrap(MethodValidator.class);
If not stated otherwise, none of this interface's methods allow null
as parameter value.
|
|
|
|
|
|