Class MethodValidationAdapter
java.lang.Object
org.springframework.validation.beanvalidation.MethodValidationAdapter
Assist with applying method-level validation via
Validator
, adapt each resulting
ConstraintViolation
to ParameterValidationResult
, and
raise MethodValidationException
.
Used by MethodValidationInterceptor
.
- Since:
- 6.1
- Author:
- Rossen Stoyanchev
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Contract to determine the object name of an@Valid
method parameter. -
Constructor Summary
ConstructorDescriptionCreate an instance using a default JSR-303 validator underneath.MethodValidationAdapter
(Validator validator) Create an instance using the given JSR-303 Validator.MethodValidationAdapter
(ValidatorFactory validatorFactory) Create an instance using the given JSR-303 ValidatorFactory.MethodValidationAdapter
(Supplier<Validator> validator) Create an instance for the supplied (potentially lazily initialized) Validator. -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>[]
determineValidationGroups
(Object target, Method method) Use this method determine the validation groups to pass intovalidateMethodArguments(Object, Method, MethodParameter[], Object[], Class[])
andvalidateMethodReturnValue(Object, Method, MethodParameter, Object, Class[])
.Return theconfigured
MessageCodesResolver
.Return theconfigured
ParameterNameDiscoverer
.void
Configure a resolver forBindingResult
method parameters to match the behavior of the higher level programming model, e.g.void
setMessageCodesResolver
(MessageCodesResolver messageCodesResolver) Set the strategy to use to determine message codes for violations.void
setParameterNameDiscoverer
(ParameterNameDiscoverer parameterNameDiscoverer) Set the ParameterNameDiscoverer to use to resolve method parameter names that is in turn used to create error codes forMessageSourceResolvable
.validateMethodArguments
(Object target, Method method, MethodParameter[] parameters, Object[] arguments, Class<?>[] groups) Validate the given method arguments and return the result of validation.validateMethodReturnValue
(Object target, Method method, MethodParameter returnType, Object returnValue, Class<?>[] groups) Validate the given return value and return the result of validation.
-
Constructor Details
-
MethodValidationAdapter
public MethodValidationAdapter()Create an instance using a default JSR-303 validator underneath. -
MethodValidationAdapter
Create an instance using the given JSR-303 ValidatorFactory.- Parameters:
validatorFactory
- the JSR-303 ValidatorFactory to use
-
MethodValidationAdapter
Create an instance using the given JSR-303 Validator.- Parameters:
validator
- the JSR-303 Validator to use
-
MethodValidationAdapter
Create an instance for the supplied (potentially lazily initialized) Validator.- Parameters:
validator
- a Supplier for the Validator to use
-
-
Method Details
-
setMessageCodesResolver
Set the strategy to use to determine message codes for violations.Default is a DefaultMessageCodesResolver.
-
getMessageCodesResolver
Return theconfigured
MessageCodesResolver
. -
setParameterNameDiscoverer
Set the ParameterNameDiscoverer to use to resolve method parameter names that is in turn used to create error codes forMessageSourceResolvable
.Default is
DefaultParameterNameDiscoverer
. -
getParameterNameDiscoverer
Return theconfigured
ParameterNameDiscoverer
. -
setBindingResultNameResolver
public void setBindingResultNameResolver(MethodValidationAdapter.BindingResultNameResolver nameResolver) Configure a resolver forBindingResult
method parameters to match the behavior of the higher level programming model, e.g. how the name of@ModelAttribute
or@RequestBody
is determined in Spring MVC.If this is not configured, then
createBindingResult(org.springframework.core.MethodParameter, java.lang.Object)
will apply default behavior to resolve the name to use. behavior applies.- Parameters:
nameResolver
- the resolver to use
-
determineValidationGroups
Use this method determine the validation groups to pass intovalidateMethodArguments(Object, Method, MethodParameter[], Object[], Class[])
andvalidateMethodReturnValue(Object, Method, MethodParameter, Object, Class[])
.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:
target
- the target Objectmethod
- the target method- Returns:
- the applicable validation groups as a
Class
array
-
validateMethodArguments
public MethodValidationResult validateMethodArguments(Object target, Method method, @Nullable MethodParameter[] parameters, Object[] arguments, Class<?>[] groups) Validate the given method arguments and return the result of validation.- Parameters:
target
- the target Objectmethod
- the target methodparameters
- the parameters, if already created and availablearguments
- the candidate argument values to validategroups
- groups for validation determined viadetermineValidationGroups(Object, Method)
- Returns:
- a result with
violations
andvalidationResults
, both possibly empty in case there are no violations
-
validateMethodReturnValue
public MethodValidationResult validateMethodReturnValue(Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue, Class<?>[] groups) Validate the given return value and return the result of validation.- Parameters:
target
- the target Objectmethod
- the target methodreturnType
- the return parameter, if already created and availablereturnValue
- the return value to validategroups
- groups for validation determined viadetermineValidationGroups(Object, Method)
- Returns:
- a result with
violations
andvalidationResults
, both possibly empty in case there are no violations
-