To be eligible for Spring-driven method validation, target classes need to be annotated
with Spring’s @Validated annotation, which can optionally also declare the validation
groups to use. See
MethodValidationPostProcessor
for setup details with the Hibernate Validator and Bean Validation providers.
Method validation relies on AOP Proxies around the
target classes, either JDK dynamic proxies for methods on interfaces or CGLIB proxies.
There are certain limitations with the use of proxies, some of which are described in
Understanding AOP Proxies. In addition remember
to always use methods and accessors on proxied classes; direct field access will not work.
Spring MVC and WebFlux have built-in support for the same underlying method validation but without
the need for AOP. Therefore, do check the rest of this section, and also see the Spring MVC
Validation and
Error Responses sections, and the WebFlux
Validation and
Error Responses sections.
By default, jakarta.validation.ConstraintViolationException is raised with the set of
ConstraintViolations returned by jakarta.validation.Validator. As an alternative,
you can have MethodValidationException raised instead with ConstraintViolations
adapted to MessageSourceResolvable errors. To enable set the following flag:
@Bean
public static MethodValidationPostProcessor validationPostProcessor() {
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
processor.setAdaptConstraintViolations(true);
return processor;
@JvmStatic
fun validationPostProcessor() = MethodValidationPostProcessor().apply {
setAdaptConstraintViolations(true)
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="adaptConstraintViolations" value="true"/>
</bean>
MethodValidationException contains a list of ParameterValidationResults which
group errors by method parameter, and each exposes a MethodParameter, the argument
value, and a list of MessageSourceResolvable errors adapted from
ConstraintViolations. For @Valid method parameters with cascaded violations on
fields and properties, the ParameterValidationResult is ParameterErrors which
implements org.springframework.validation.Errors and exposes validation errors as
FieldErrors.
The adapted MessageSourceResolvable errors can be turned into error messages to
display to users through the configured MessageSource with locale and language specific
resource bundles. This section provides an example for illustration.
Given the following class declarations:
To customize the default message, you can add properties to
MessageSource
resource bundles using any of the above errors codes and message arguments. Note also that the
message argument "name" is itself a MessageSourceResolvable with error codes
"person.name" and "name" and can be customized too. For example:
Properties
Size.person.name=Please, provide a {0} that is between {2} and {1} characters long
person.name=username
The default LocalValidatorFactoryBean configuration suffices for most
cases. There are a number of configuration options for various Bean Validation
constructs, from message interpolation to traversal resolution. See the
LocalValidatorFactoryBean
javadoc for more information on these options.
You can configure a DataBinder instance with a Validator. Once configured, you can
invoke the Validator by calling binder.validate(). Any validation Errors are
automatically added to the binder’s BindingResult.
The following example shows how to use a DataBinder programmatically to invoke validation
logic after binding to a target object:
Foo target = new Foo();
DataBinder binder = new DataBinder(target);
binder.setValidator(new FooValidator());
// bind to the target object
binder.bind(propertyValues);
// validate the target object
binder.validate();
// get BindingResult that includes any validation errors
BindingResult results = binder.getBindingResult();
You can also configure a DataBinder with multiple Validator instances through
dataBinder.addValidators and dataBinder.replaceValidators. This is useful when
combining globally configured bean validation with a Spring Validator configured
locally on a DataBinder instance. See
Spring MVC Validation Configuration.
Apache®, Apache Tomcat®, Apache Kafka®, Apache Cassandra™, and Apache Geode™ are trademarks or registered trademarks of the Apache Software Foundation in the United States and/or other countries. Java™, Java™ SE, Java™ EE, and OpenJDK™ are trademarks of Oracle and/or its affiliates. Kubernetes® is a registered trademark of the Linux Foundation in the United States and other countries. Linux® is the registered trademark of Linus Torvalds in the United States and other countries. Windows® and Microsoft® Azure are registered trademarks of Microsoft Corporation. “AWS” and “Amazon Web Services” are trademarks or registered trademarks of Amazon.com Inc. or its affiliates. All other trademarks and copyrights are property of their respective owners and are only mentioned for informative purposes. Other names may be trademarks of their respective owners.