Class MethodValidationAdapter
java.lang.Object
org.springframework.validation.beanvalidation.MethodValidationAdapter
- All Implemented Interfaces:
MethodValidator
MethodValidator
that uses a Bean Validation
Validator
for validation, and adapts
ConstraintViolation
s to MethodValidationResult
.- Since:
- 6.1
- Author:
- Rossen Stoyanchev
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Strategy to resolve the name of an@Valid
method parameter to use for itsBindingResult
. -
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 TypeMethodDescriptionClass<?>[]
determineValidationGroups
(Object target, Method method) Use this method to determine the validation groups.Return theconfigured
MessageCodesResolver
.Return theconfigured
ParameterNameDiscoverer
.final Set<ConstraintViolation<Object>>
invokeValidatorForArguments
(Object target, Method method, Object[] arguments, Class<?>[] groups) Invoke the validator, and return the resulting violations.final Set<ConstraintViolation<Object>>
invokeValidatorForReturnValue
(Object target, Method method, Object returnValue, Class<?>[] groups) Invoke the validator, and return the resulting violations.void
setMessageCodesResolver
(MessageCodesResolver messageCodesResolver) Set the strategy to use to determine message codes for violations.void
Configure a resolver to determine the name of an@Valid
method parameter to use for itsBindingResult
.void
setParameterNameDiscoverer
(ParameterNameDiscoverer parameterNameDiscoverer) Set theParameterNameDiscoverer
to discover method parameter names with to create error codes forMessageSourceResolvable
.final MethodValidationResult
validateArguments
(Object target, Method method, MethodParameter[] parameters, Object[] arguments, Class<?>[] groups) Validate the given method arguments and return validation results.final MethodValidationResult
validateReturnValue
(Object target, Method method, MethodParameter returnType, Object returnValue, Class<?>[] groups) Validate the given return value and return the result of validation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.validation.method.MethodValidator
applyArgumentValidation, applyReturnValueValidation
-
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 theParameterNameDiscoverer
to discover method parameter names with to create error codes forMessageSourceResolvable
. Used only whenMethodParameter
s are not passed intovalidateArguments(java.lang.Object, java.lang.reflect.Method, org.springframework.core.MethodParameter[], java.lang.Object[], java.lang.Class<?>[])
orvalidateReturnValue(java.lang.Object, java.lang.reflect.Method, org.springframework.core.MethodParameter, java.lang.Object, java.lang.Class<?>[])
.Default is
DefaultParameterNameDiscoverer
. -
getParameterNameDiscoverer
Return theconfigured
ParameterNameDiscoverer
. -
setObjectNameResolver
Configure a resolver to determine the name of an@Valid
method parameter to use for itsBindingResult
. This allows aligning with a higher level programming model such as to resolve the name of an@ModelAttribute
method parameter in Spring MVC.By default, the object name is resolved through:
MethodParameter.getParameterName()
for input parametersConventions.getVariableNameForReturnType(Method, Class, Object)
for a return type
"{methodName}.arg{index}"
for input parameters"{methodName}.returnValue"
for a return type
-
determineValidationGroups
Use this method to determine the validation groups..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.- Specified by:
determineValidationGroups
in interfaceMethodValidator
- Parameters:
target
- the target Objectmethod
- the target method- Returns:
- the applicable validation groups as a
Class
array
-
validateArguments
public final MethodValidationResult validateArguments(Object target, Method method, @Nullable MethodParameter[] parameters, Object[] arguments, Class<?>[] groups) Description copied from interface:MethodValidator
Validate the given method arguments and return validation results.- Specified by:
validateArguments
in interfaceMethodValidator
- Parameters:
target
- the target Objectmethod
- the target methodparameters
- the parameters, if already created and availablearguments
- the candidate argument values to validategroups
- validation groups fromMethodValidator.determineValidationGroups(java.lang.Object, java.lang.reflect.Method)
- Returns:
- the result of validation
-
invokeValidatorForArguments
public final Set<ConstraintViolation<Object>> invokeValidatorForArguments(Object target, Method method, Object[] arguments, Class<?>[] groups) Invoke the validator, and return the resulting violations. -
validateReturnValue
public final MethodValidationResult validateReturnValue(Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue, Class<?>[] groups) Description copied from interface:MethodValidator
Validate the given return value and return the result of validation.- Specified by:
validateReturnValue
in interfaceMethodValidator
- Parameters:
target
- the target Objectmethod
- the target methodreturnType
- the return parameter, if already created and availablereturnValue
- the return value to validategroups
- validation groups fromMethodValidator.determineValidationGroups(java.lang.Object, java.lang.reflect.Method)
- Returns:
- the result of validation
-
invokeValidatorForReturnValue
public final Set<ConstraintViolation<Object>> invokeValidatorForReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups) Invoke the validator, and return the resulting violations.
-