Top Description Fields Constructors Methods
org.springframework.validation.beanvalidation

public Class MethodValidationPostProcessor

extends AbstractBeanFactoryAwareAdvisingPostProcessor
implements InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean
Annotations
@SuppressWarnings:serial
Imports
java.lang.annotation.Annotation, java.util.function.Supplier, jakarta.validation.ConstraintViolation, .ConstraintViolationException, .Validation, .Validator, .ValidatorFactory, org.aopalliance.aop.Advice, org.springframework.aop.Pointcut, org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor, org.springframework.aop.support.DefaultPointcutAdvisor, org.springframework.aop.support.annotation.AnnotationMatchingPointcut, org.springframework.beans.factory.InitializingBean, .ObjectProvider, org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.util.Assert, org.springframework.util.function.SingletonSupplier, org.springframework.validation.annotation.Validated, org.springframework.validation.method.MethodValidationException, .MethodValidationResult

A convenient BeanPostProcessor implementation that delegates to a JSR-303 provider for performing method-level validation on annotated methods.

Applicable methods have JSR-303 constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation), e.g.:

public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)

In case of validation errors, the interceptor can raise ConstraintViolationException, or adapt the violations to MethodValidationResult and raise MethodValidationException.

Target classes with such annotated methods need to be annotated with Spring's Validated annotation at the type level, for their methods to be searched for inline constraint annotations. Validation groups can be specified through @Validated as well. By default, JSR-303 will validate against its default group only.

As of Spring 5.0, this functionality requires a Bean Validation 1.1+ provider.

Author
Juergen Hoeller
Since
3.1
See Also
MethodValidationInterceptor, jakarta.validation.executable.ExecutableValidator

Field Summary

Modifier and TypeField and Description
private boolean
private Class<? extends Annotation>
private Supplier<Validator>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

protected Advice

Returns:

the interceptor to use (typically, but not necessarily, a MethodValidationInterceptor or subclass thereof)
createMethodValidationAdvice
(Supplier<Validator>
a Supplier for the Validator to use
validator
)

Create AOP advice for method validation purposes, to be applied with a pointcut for the specified 'validated' annotation.

public void
setAdaptConstraintViolations(boolean adaptViolations)

Whether to adapt ConstraintViolations to MethodValidationResult.

public void
setValidatedAnnotationType(Class<? extends Annotation>
the desired annotation type
validatedAnnotationType
)

Set the 'validated' annotation type.

public void
setValidator(Validator validator)

Set the JSR-303 Validator to delegate to for validating methods.

public void
setValidatorFactory(ValidatorFactory validatorFactory)

Set the JSR-303 ValidatorFactory to delegate to for validating methods, using its default Validator.

public void
setValidatorProvider(ObjectProvider<Validator> validatorProvider)

Set a lazily initialized Validator to delegate to for validating methods.

Inherited from org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor:
isEligibleprepareProxyFactorysetBeanFactory

Field Detail

adaptConstraintViolationsback to summary
private boolean adaptConstraintViolations
validatedAnnotationTypeback to summary
private Class<? extends Annotation> validatedAnnotationType
validatorback to summary
private Supplier<Validator> validator

Constructor Detail

MethodValidationPostProcessorback to summary
public MethodValidationPostProcessor()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
createMethodValidationAdviceback to summary
protected Advice createMethodValidationAdvice(Supplier<Validator> validator)

Create AOP advice for method validation purposes, to be applied with a pointcut for the specified 'validated' annotation.

Parameters
validator:Supplier<Validator>

a Supplier for the Validator to use

Returns:Advice

the interceptor to use (typically, but not necessarily, a MethodValidationInterceptor or subclass thereof)

Since
6.0
setAdaptConstraintViolationsback to summary
public void setAdaptConstraintViolations(boolean adaptViolations)

Whether to adapt ConstraintViolations to MethodValidationResult.

By default false in which case jakarta.validation.ConstraintViolationException is raised in case of violations. When set to true, MethodValidationException is raised instead with the method validation results.

Since
6.1
setValidatedAnnotationTypeback to summary
public void setValidatedAnnotationType(Class<? extends Annotation> validatedAnnotationType)

Set the 'validated' annotation type. The default validated annotation type is the Validated annotation.

This setter property exists so that developers can provide their own (non-Spring-specific) annotation type to indicate that a class is supposed to be validated in the sense of applying method validation.

Parameters
validatedAnnotationType:Class<? extends Annotation>

the desired annotation type

setValidatorback to summary
public void setValidator(Validator validator)

Set the JSR-303 Validator to delegate to for validating methods.

Default is the default ValidatorFactory's default Validator.

setValidatorFactoryback to summary
public void setValidatorFactory(ValidatorFactory validatorFactory)

Set the JSR-303 ValidatorFactory to delegate to for validating methods, using its default Validator.

Default is the default ValidatorFactory's default Validator.

See Also
jakarta.validation.ValidatorFactory#getValidator()
setValidatorProviderback to summary
public void setValidatorProvider(ObjectProvider<Validator> validatorProvider)

Set a lazily initialized Validator to delegate to for validating methods.

Since
6.0
See Also
setValidator