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
ConstraintViolations to MethodValidationResult.- Since:
- 6.1
- Author:
- Rossen Stoyanchev
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceStrategy to resolve the name of an@Validmethod parameter to use for itsBindingResult. -
Constructor Summary
ConstructorsConstructorDescriptionCreate 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) Determine the applicable validation groups.Return theconfiguredMessageCodesResolver.Return theconfiguredParameterNameDiscoverer.Return theSpringValidatorAdapterconfigured for use.final Set<ConstraintViolation<Object>>invokeValidatorForArguments(Object target, Method method, @Nullable Object[] arguments, Class<?>[] groups) Invoke the validator, and return the resulting violations.final Set<ConstraintViolation<Object>>invokeValidatorForReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups) Invoke the validator, and return the resulting violations.voidsetMessageCodesResolver(MessageCodesResolver messageCodesResolver) Set the strategy to use to determine message codes for violations.voidConfigure a resolver to determine the name of an@Validmethod parameter to use for itsBindingResult.voidsetParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) Set theParameterNameDiscovererto discover method parameter names with to create error codes forMessageSourceResolvable.final MethodValidationResultvalidateArguments(Object target, Method method, MethodParameter @Nullable [] parameters, @Nullable Object[] arguments, Class<?>[] groups) Validate the given method arguments and return validation results.final MethodValidationResultvalidateReturnValue(Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue, Class<?>[] groups) Validate the given return value and return validation results.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
-
getSpringValidatorAdapter
Return theSpringValidatorAdapterconfigured for use. -
setMessageCodesResolver
Set the strategy to use to determine message codes for violations.Default is a DefaultMessageCodesResolver.
-
getMessageCodesResolver
Return theconfiguredMessageCodesResolver. -
setParameterNameDiscoverer
Set theParameterNameDiscovererto discover method parameter names with to create error codes forMessageSourceResolvable. Used only whenMethodParameters 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 theconfiguredParameterNameDiscoverer. -
setObjectNameResolver
Configure a resolver to determine the name of an@Validmethod parameter to use for itsBindingResult. This allows aligning with a higher level programming model such as to resolve the name of an@ModelAttributemethod 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
Determine the applicable validation groups. By default, obtained from an@Validatedannotation on the method, or on the class level.Default are the validation groups as specified in the
Validatedannotation 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:
determineValidationGroupsin interfaceMethodValidator- Parameters:
target- the target Objectmethod- the target method- Returns:
- the applicable validation groups as a
Classarray
-
validateArguments
public final MethodValidationResult validateArguments(Object target, Method method, MethodParameter @Nullable [] parameters, @Nullable Object[] arguments, Class<?>[] groups) Description copied from interface:MethodValidatorValidate the given method arguments and return validation results.- Specified by:
validateArgumentsin 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, @Nullable 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:MethodValidatorValidate the given return value and return validation results.- Specified by:
validateReturnValuein 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.
-