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 ConstraintViolations to MethodValidationResult.
Since:
6.1
Author:
Rossen Stoyanchev
  • Constructor Details

    • MethodValidationAdapter

      public MethodValidationAdapter()
      Create an instance using a default JSR-303 validator underneath.
    • MethodValidationAdapter

      public MethodValidationAdapter(jakarta.validation.ValidatorFactory validatorFactory)
      Create an instance using the given JSR-303 ValidatorFactory.
      Parameters:
      validatorFactory - the JSR-303 ValidatorFactory to use
    • MethodValidationAdapter

      public MethodValidationAdapter(jakarta.validation.Validator validator)
      Create an instance using the given JSR-303 Validator.
      Parameters:
      validator - the JSR-303 Validator to use
    • MethodValidationAdapter

      public MethodValidationAdapter(Supplier<jakarta.validation.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 MethodParameters are not passed into validateArguments(Object, Method, MethodParameter[], Object[], Class[]) or validateReturnValue(Object, Method, MethodParameter, Object, 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, org.springframework.core.MethodParameter @Nullable [] parameters, @Nullable 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(Object, Method)
      Returns:
      the result of validation
    • invokeValidatorForArguments

      public final Set<jakarta.validation.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 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(Object, Method)
      Returns:
      the result of validation
    • invokeValidatorForReturnValue

      public final Set<jakarta.validation.ConstraintViolation<Object>> invokeValidatorForReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups)
      Invoke the validator, and return the resulting violations.