public class MethodValidationInterceptor extends Object implements MethodInterceptor
MethodInterceptor
implementation that delegates to a
JSR-303 provider for performing method-level validation on annotated methods.
Applicable methods have JSR-303 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)
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.
MethodValidationPostProcessor
,
ExecutableValidator
Constructor and Description |
---|
MethodValidationInterceptor()
Create a new MethodValidationInterceptor using a default JSR-303 validator underneath.
|
MethodValidationInterceptor(Validator validator)
Create a new MethodValidationInterceptor using the given JSR-303 Validator.
|
MethodValidationInterceptor(ValidatorFactory validatorFactory)
Create a new MethodValidationInterceptor using the given JSR-303 ValidatorFactory.
|
Modifier and Type | Method and Description |
---|---|
protected Class<?>[] |
determineValidationGroups(MethodInvocation invocation)
Determine the validation groups to validate against for the given method invocation.
|
Object |
invoke(MethodInvocation invocation)
Implement this method to perform extra treatments before and
after the invocation.
|
public MethodValidationInterceptor()
public MethodValidationInterceptor(ValidatorFactory validatorFactory)
validatorFactory
- the JSR-303 ValidatorFactory to usepublic MethodValidationInterceptor(Validator validator)
validator
- the JSR-303 Validator to usepublic Object invoke(MethodInvocation invocation) throws Throwable
MethodInterceptor
Joinpoint.proceed()
.invoke
in interface MethodInterceptor
invocation
- the method invocation joinpointJoinpoint.proceed()
;
might be intercepted by the interceptorThrowable
- if the interceptors or the target object
throws an exceptionprotected Class<?>[] determineValidationGroups(MethodInvocation invocation)
Default are the validation groups as specified in the Validated
annotation
on the containing target class of the method.
invocation
- the current MethodInvocation