Top Description Inners Fields Constructors Methods
org.springframework.validation.beanvalidation

public Class MethodValidationInterceptor

extends Object
implements MethodInterceptor
Class Inheritance
All Implemented Interfaces
org.aopalliance.intercept.MethodInterceptor, org.aopalliance.intercept.Interceptor, org.aopalliance.aop.Advice
Imports
java.lang.reflect.Method, .Parameter, java.util.Collections, .List, .Set, java.util.function.Supplier, jakarta.validation.ConstraintViolation, .ConstraintViolationException, .Valid, .Validator, .ValidatorFactory, org.aopalliance.intercept.MethodInterceptor, .MethodInvocation, reactor.core.publisher.Flux, .Mono, org.springframework.aop.ProxyMethodInvocation, org.springframework.beans.factory.FactoryBean, .SmartFactoryBean, org.springframework.core.MethodParameter, .ReactiveAdapter, .ReactiveAdapterRegistry, org.springframework.core.annotation.AnnotationUtils, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, org.springframework.validation.BeanPropertyBindingResult, .Errors, org.springframework.validation.annotation.Validated, org.springframework.validation.method.MethodValidationException, .MethodValidationResult, .ParameterErrors, .ParameterValidationResult

An AOP Alliance MethodInterceptor implementation that delegates to a JSR-303 provider for performing method-level validation on annotated methods.

Applicable methods have jakarta.validation.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.

Authors
Juergen Hoeller, Rossen Stoyanchev
Since
3.1
See Also
MethodValidationPostProcessor, jakarta.validation.executable.ExecutableValidator

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MethodValidationInterceptor.ReactorValidationHelper

Helper class to decorate reactive arguments with async validation.

Field Summary

Modifier and TypeField and Description
private final boolean
private static final boolean
private final MethodValidationAdapter

Constructor Summary

AccessConstructor and Description
public
MethodValidationInterceptor()

Create a new MethodValidationInterceptor using a default JSR-303 validator underneath.

public
MethodValidationInterceptor(ValidatorFactory
the JSR-303 ValidatorFactory to use
validatorFactory
)

Create a new MethodValidationInterceptor using the given JSR-303 ValidatorFactory.

public
MethodValidationInterceptor(Validator
the JSR-303 Validator to use
validator
)

Create a new MethodValidationInterceptor using the given JSR-303 Validator.

public
MethodValidationInterceptor(Supplier<Validator>
a Supplier for the Validator to use
validator
)

Create a new MethodValidationInterceptor for the supplied (potentially lazily initialized) Validator.

public
MethodValidationInterceptor(Supplier<Validator>
a Supplier for the Validator to use
validator
,
boolean
whether to adapt ConstraintViolations, and if true, raise MethodValidationException, of if false raise ConstraintViolationException instead
adaptViolations
)

Create a new MethodValidationInterceptor for the supplied (potentially lazily initialized) Validator.

private
MethodValidationInterceptor(MethodValidationAdapter validationAdapter, boolean adaptViolations)

Method Summary

Modifier and TypeMethod and Description
protected Class<?>[]

Returns:

the applicable validation groups as a Class array
determineValidationGroups
(MethodInvocation
the current MethodInvocation
invocation
)

Determine the validation groups to validate against for the given method invocation.

private static Object
public Object
invoke(MethodInvocation
the method invocation joinpoint
invocation
)

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

adaptViolationsback to summary
private final boolean adaptViolations
REACTOR_PRESENTback to summary
private static final boolean REACTOR_PRESENT
validationAdapterback to summary
private final MethodValidationAdapter validationAdapter

Constructor Detail

MethodValidationInterceptorback to summary
public MethodValidationInterceptor()

Create a new MethodValidationInterceptor using a default JSR-303 validator underneath.

MethodValidationInterceptorback to summary
public MethodValidationInterceptor(ValidatorFactory validatorFactory)

Create a new MethodValidationInterceptor using the given JSR-303 ValidatorFactory.

Parameters
validatorFactory:ValidatorFactory

the JSR-303 ValidatorFactory to use

MethodValidationInterceptorback to summary
public MethodValidationInterceptor(Validator validator)

Create a new MethodValidationInterceptor using the given JSR-303 Validator.

Parameters
validator:Validator

the JSR-303 Validator to use

MethodValidationInterceptorback to summary
public MethodValidationInterceptor(Supplier<Validator> validator)

Create a new MethodValidationInterceptor for the supplied (potentially lazily initialized) Validator.

Parameters
validator:Supplier<Validator>

a Supplier for the Validator to use

Since
6.0
MethodValidationInterceptorback to summary
public MethodValidationInterceptor(Supplier<Validator> validator, boolean adaptViolations)

Create a new MethodValidationInterceptor for the supplied (potentially lazily initialized) Validator.

Parameters
validator:Supplier<Validator>

a Supplier for the Validator to use

adaptViolations:boolean

whether to adapt ConstraintViolations, and if true, raise MethodValidationException, of if false raise ConstraintViolationException instead

Since
6.1
MethodValidationInterceptorback to summary
private MethodValidationInterceptor(MethodValidationAdapter validationAdapter, boolean adaptViolations)

Method Detail

determineValidationGroupsback to summary
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.

Parameters
invocation:MethodInvocation

the current MethodInvocation

Returns:Class<?>[]

the applicable validation groups as a Class array

getTargetback to summary
private static Object getTarget(MethodInvocation invocation)
invokeback to summary
public Object invoke(MethodInvocation invocation) throws Throwable

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invoke Joinpoint#proceed().

Parameters
invocation:MethodInvocation

the method invocation joinpoint

Returns:Object

the result of the call to Joinpoint#proceed(); might be intercepted by the interceptor

Annotations
@Override
@Nullable
Exceptions
Throwable:
if the interceptors or the target object throws an exception
isFactoryBeanMetadataMethodback to summary
private boolean isFactoryBeanMetadataMethod(Method method)
org.springframework.validation.beanvalidation back to summary

private final Class MethodValidationInterceptor.ReactorValidationHelper

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.validation.beanvalidation.MethodValidationInterceptor.ReactorValidationHelper

Helper class to decorate reactive arguments with async validation.

Field Summary

Modifier and TypeField and Description
private static final ReactiveAdapterRegistry

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Class<?>[]
pack-priv static Object[]
insertAsyncValidation(Supplier<SpringValidatorAdapter> validatorAdapterSupplier, boolean adaptViolations, Object target, Method method, Object[] arguments)

private static <T> void
validate(SpringValidatorAdapter validatorAdapter, boolean adaptViolations, Object target, Method method, MethodParameter parameter, Object argument, Class<?>[] groups)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

reactiveAdapterRegistryback to summary
private static final ReactiveAdapterRegistry reactiveAdapterRegistry

Constructor Detail

ReactorValidationHelperback to summary
private ReactorValidationHelper()

Method Detail

determineValidationGroupsback to summary
private static Class<?>[] determineValidationGroups(Parameter parameter)
Annotations
@Nullable
insertAsyncValidationback to summary
pack-priv static Object[] insertAsyncValidation(Supplier<SpringValidatorAdapter> validatorAdapterSupplier, boolean adaptViolations, Object target, Method method, Object[] arguments)
validateback to summary
private static <T> void validate(SpringValidatorAdapter validatorAdapter, boolean adaptViolations, Object target, Method method, MethodParameter parameter, Object argument, Class<?>[] groups)
Annotations
@SuppressWarnings:unchecked