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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceContract to determine the object name of an@Validmethod parameter. -
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 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 theconfiguredMessageCodesResolver.Return theconfiguredParameterNameDiscoverer.voidConfigure a resolver forBindingResultmethod parameters to match the behavior of the higher level programming model, e.g.voidsetMessageCodesResolver(MessageCodesResolver messageCodesResolver) Set the strategy to use to determine message codes for violations.voidsetParameterNameDiscoverer(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 theconfiguredMessageCodesResolver. -
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 theconfiguredParameterNameDiscoverer. -
setBindingResultNameResolver
public void setBindingResultNameResolver(MethodValidationAdapter.BindingResultNameResolver nameResolver) Configure a resolver forBindingResultmethod parameters to match the behavior of the higher level programming model, e.g. how the name of@ModelAttributeor@RequestBodyis 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
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.- Parameters:
target- the target Objectmethod- the target method- Returns:
- the applicable validation groups as a
Classarray
-
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
violationsandvalidationResults, 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
violationsandvalidationResults, both possibly empty in case there are no violations
-