Package org.springframework.validation.beanvalidation

# Class MethodValidationAdapter

java.lang.Object
org.springframework.validation.beanvalidation.MethodValidationAdapter

All Implemented Interfaces:
  `MethodValidator`

public class MethodValidationAdapter extends Object implements 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

Nested Classes
Modifier and Type
Class
Description
`static interface`
`MethodValidationAdapter.ObjectNameResolver`
Strategy to resolve the name of an `@Valid` method parameter to use for its `BindingResult`.

## Constructor Summary

Constructors
Constructor
Description
`MethodValidationAdapter()`
Create 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 Type
Method
Description
`Class<?>[]`
`determineValidationGroups(Object target, Method method)`
Determine the applicable validation groups.
`MessageCodesResolver`
`getMessageCodesResolver()`
Return the `configured` `MessageCodesResolver`.
`org.springframework.core.ParameterNameDiscoverer`
`getParameterNameDiscoverer()`
Return the `configured` `ParameterNameDiscoverer`.
`Supplier<SpringValidatorAdapter>`
`getSpringValidatorAdapter()`
Return the `SpringValidatorAdapter` configured for use.
`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`
`setObjectNameResolver(MethodValidationAdapter.ObjectNameResolver nameResolver)`
Configure a resolver to determine the name of an `@Valid` method parameter to use for its `BindingResult`.
`void`
`setParameterNameDiscoverer(org.springframework.core.ParameterNameDiscoverer parameterNameDiscoverer)`
Set the `ParameterNameDiscoverer` to discover method parameter names with to create error codes for `MessageSourceResolvable`.
`final MethodValidationResult`
`validateArguments(Object target, Method method, org.springframework.core.MethodParameter[] parameters, Object[] arguments, Class<?>[] groups)`
Validate the given method arguments and return validation results.
`final MethodValidationResult`
`validateReturnValue(Object target, Method method, org.springframework.core.MethodParameter returnType, 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, 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

public MethodValidationAdapter(ValidatorFactory validatorFactory)
Create an instance using the given JSR-303 ValidatorFactory.

Parameters:
  `validatorFactory` - the JSR-303 ValidatorFactory to use

### MethodValidationAdapter

public MethodValidationAdapter(Validator validator)
Create an instance using the given JSR-303 Validator.

Parameters:
  `validator` - the JSR-303 Validator to use

### MethodValidationAdapter

public MethodValidationAdapter(Supplier<Validator> validator)
Create an instance for the supplied (potentially lazily initialized) Validator.

Parameters:
  `validator` - a Supplier for the Validator to use

## Method Details

### getSpringValidatorAdapter

public Supplier<SpringValidatorAdapter> getSpringValidatorAdapter()
Return the `SpringValidatorAdapter` configured for use.

### setMessageCodesResolver

public void setMessageCodesResolver(MessageCodesResolver messageCodesResolver)
Set the strategy to use to determine message codes for violations.

Default is a DefaultMessageCodesResolver.

### getMessageCodesResolver

public MessageCodesResolver getMessageCodesResolver()
Return the `configured` `MessageCodesResolver`.

### setParameterNameDiscoverer

public void setParameterNameDiscoverer(org.springframework.core.ParameterNameDiscoverer parameterNameDiscoverer)
Set the `ParameterNameDiscoverer` to discover method parameter names with to create error codes for `MessageSourceResolvable`. Used only when `MethodParameter`s are not passed into `validateArguments(java.lang.Object, java.lang.reflect.Method, org.springframework.core.MethodParameter[], java.lang.Object[], java.lang.Class<?>[])` or `validateReturnValue(java.lang.Object, java.lang.reflect.Method, org.springframework.core.MethodParameter, java.lang.Object, java.lang.Class<?>[])`.

Default is `DefaultParameterNameDiscoverer`.

### getParameterNameDiscoverer

public org.springframework.core.ParameterNameDiscoverer getParameterNameDiscoverer()
Return the `configured` `ParameterNameDiscoverer`.

### setObjectNameResolver

public void setObjectNameResolver(MethodValidationAdapter.ObjectNameResolver nameResolver)
Configure a resolver to determine the name of an `@Valid` method parameter to use for its `BindingResult`. 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 parameters
- `Conventions.getVariableNameForReturnType(Method, Class, Object)` for a return type

If a name cannot be determined, for example, a return value with insufficient type information, then it defaults to one of:

- `"{methodName}.arg{index}"` for input parameters
- `"{methodName}.returnValue"` for a return type

### determineValidationGroups

public Class<?>[] determineValidationGroups(Object target, Method method)
Determine the applicable validation groups. By default, obtained from an `@Validated` annotation on the method, or on the class level.

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 interface `MethodValidator`
Parameters:
  `target` - the target Object
  `method` - the target method
Returns:
  the applicable validation groups as a `Class` array

### validateArguments

public final MethodValidationResult validateArguments(Object target, Method method, @Nullable org.springframework.core.MethodParameter[] parameters, Object[] arguments, Class<?>[] groups)
Description copied from interface: `MethodValidator`
Validate the given method arguments and return validation results.

Specified by:
  `validateArguments` in interface `MethodValidator`
Parameters:
  `target` - the target Object
  `method` - the target method
  `parameters` - the parameters, if already created and available
  `arguments` - the candidate argument values to validate
  `groups` - validation groups from `MethodValidator.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 org.springframework.core.MethodParameter returnType, @Nullable Object returnValue, Class<?>[] groups)
Description copied from interface: `MethodValidator`
Validate the given return value and return validation results.

Specified by:
  `validateReturnValue` in interface `MethodValidator`
Parameters:
  `target` - the target Object
  `method` - the target method
  `returnType` - the return parameter, if already created and available
  `returnValue` - the return value to validate
  `groups` - validation groups from `MethodValidator.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.