Class MethodValidationInterceptor

All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor

public class MethodValidationInterceptor extends Object implements MethodInterceptor
An AOP Alliance MethodInterceptor implementation that delegates to a JSR-303 provider for performing method-level validation on annotated methods.

Applicable methods have Constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation).

E.g.: public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)

In case of validation errors, the interceptor can raise ConstraintViolationException, or adapt the violations to MethodValidationResult and raise MethodValidationException.

Validation groups can be specified through Spring's Validated annotation at the type level of the containing target class, applying to all public service methods of that class. By default, JSR-303 will validate against its default group only.

As of Spring 5.0, this functionality requires a Bean Validation 1.1+ provider.

Juergen Hoeller, Rossen Stoyanchev
See Also:
  • Constructor Details

    • MethodValidationInterceptor

      public MethodValidationInterceptor()
      Create a new MethodValidationInterceptor using a default JSR-303 validator underneath.
    • MethodValidationInterceptor

      public MethodValidationInterceptor(ValidatorFactory validatorFactory)
      Create a new MethodValidationInterceptor using the given JSR-303 ValidatorFactory.
      validatorFactory - the JSR-303 ValidatorFactory to use
    • MethodValidationInterceptor

      public MethodValidationInterceptor(Validator validator)
      Create a new MethodValidationInterceptor using the given JSR-303 Validator.
      validator - the JSR-303 Validator to use
    • MethodValidationInterceptor

      public MethodValidationInterceptor(Supplier<Validator> validator)
      Create a new MethodValidationInterceptor for the supplied (potentially lazily initialized) Validator.
      validator - a Supplier for the Validator to use
    • MethodValidationInterceptor

      public MethodValidationInterceptor(Supplier<Validator> validator, boolean adaptViolations)
      Create a new MethodValidationInterceptor for the supplied (potentially lazily initialized) Validator.
      validator - a Supplier for the Validator to use
      adaptViolations - whether to adapt ConstraintViolations, and if true, raise MethodValidationException, of if false raise ConstraintViolationException instead
  • Method Details

    • invoke

      @Nullable public Object invoke(MethodInvocation invocation) throws Throwable
      Description copied from interface: MethodInterceptor
      Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invoke Joinpoint.proceed().
      Specified by:
      invoke in interface MethodInterceptor
      invocation - the method invocation joinpoint
      the result of the call to Joinpoint.proceed(); might be intercepted by the interceptor
      Throwable - if the interceptors or the target object throws an exception
    • determineValidationGroups

      protected Class<?>[] determineValidationGroups(MethodInvocation invocation)
      Determine the validation groups to validate against for the given method invocation.

      Default are the validation groups as specified in the Validated annotation on the method, or on the containing target class of the method, or for an AOP proxy without a target (with all behavior in advisors), also check on proxied interfaces.

      invocation - the current MethodInvocation
      the applicable validation groups as a Class array