Class SpringValidatorAdapter
- java.lang.Object
- org.springframework.validation.beanvalidation.SpringValidatorAdapter
- All Implemented Interfaces:
Validator
,SmartValidator
,Validator
- Direct Known Subclasses:
CustomValidatorBean
,LocalValidatorFactoryBean
public class SpringValidatorAdapter extends Object implements SmartValidator, Validator
Adapter that takes a JSR-303javax.validator.Validator
and exposes it as a SpringValidator
while also exposing the original JSR-303 Validator interface itself.Can be used as a programmatic wrapper. Also serves as base class for
CustomValidatorBean
andLocalValidatorFactoryBean
.Note that Bean Validation 1.1's
#forExecutables
method isn't supported on this adapter: We do not expect that method to be called by application code; considerMethodValidationInterceptor
instead. If you really need programmatic#forExecutables
access, call#unwrap(Validator.class)
which will provide the nativeValidator
object with#forExecutables
support.- Since:
- 3.0
- Author:
- Juergen Hoeller
Constructor Summary
Constructors Constructor Description SpringValidatorAdapter(Validator targetValidator)
Create a new SpringValidatorAdapter for the given JSR-303 Validator.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected String
determineErrorCode(ConstraintDescriptor<?> descriptor)
Determine a Spring-reported error code for the given constraint descriptor.protected String
determineField(ConstraintViolation<Object> violation)
Determine a field for the given constraint violation.protected Object[]
getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor)
Return FieldError arguments for a validation error on the given field.BeanDescriptor
getConstraintsForClass(Class<?> clazz)
protected Object
getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult)
Extract the rejected value behind the given constraint violation, for exposure through the Spring errors representation.protected MessageSourceResolvable
getResolvableField(String objectName, String field)
Build a resolvable wrapper for the specified field, allowing to resolve the field's name in aMessageSource
.protected void
processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors)
Process the given JSR-303 ConstraintViolations, adding corresponding errors to the provided SpringErrors
object.boolean
supports(Class<?> clazz)
<T> T
unwrap(Class<T> type)
void
validate(Object target, Errors errors)
Validate the suppliedtarget
object, which must be of aClass
for which theValidator.supports(Class)
method typically has (or would) returntrue
.void
validate(Object target, Errors errors, Object... validationHints)
Validate the suppliedtarget
object, which must be of a type ofClass
for which theValidator.supports(Class)
method typically returnstrue
.<T> Set<ConstraintViolation<T>>
validate(T object, Class<?>... groups)
<T> Set<ConstraintViolation<T>>
validateProperty(T object, String propertyName, Class<?>... groups)
<T> Set<ConstraintViolation<T>>
validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)
Constructor Detail
SpringValidatorAdapter
public SpringValidatorAdapter(Validator targetValidator)
Create a new SpringValidatorAdapter for the given JSR-303 Validator.- Parameters:
targetValidator
- the JSR-303 Validator to wrap
Method Detail
validate
public void validate(Object target, Errors errors)
Description copied from interface:Validator
Validate the suppliedtarget
object, which must be of aClass
for which theValidator.supports(Class)
method typically has (or would) returntrue
.The supplied
errors
instance can be used to report any resulting validation errors.- Specified by:
validate
in interfaceValidator
- Parameters:
target
- the object that is to be validated (can benull
)errors
- contextual state about the validation process (nevernull
)- See Also:
ValidationUtils
validate
public void validate(Object target, Errors errors, Object... validationHints)
Description copied from interface:SmartValidator
Validate the suppliedtarget
object, which must be of a type ofClass
for which theValidator.supports(Class)
method typically returnstrue
.The supplied
errors
instance can be used to report any resulting validation errors.This variant of
validate()
supports validation hints, such as validation groups against a JSR-303 provider (in which case, the provided hint objects need to be annotation arguments of typeClass
).Note: Validation hints may get ignored by the actual target
Validator
, in which case this method should behave just like its regularValidator.validate(Object, Errors)
sibling.- Specified by:
validate
in interfaceSmartValidator
- Parameters:
target
- the object that is to be validated (can benull
)errors
- contextual state about the validation process (nevernull
)validationHints
- one or more hint objects to be passed to the validation engine- See Also:
ValidationUtils
processConstraintViolations
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors)
Process the given JSR-303 ConstraintViolations, adding corresponding errors to the provided SpringErrors
object.- Parameters:
violations
- the JSR-303 ConstraintViolation resultserrors
- the Spring errors object to register to
determineField
protected String determineField(ConstraintViolation<Object> violation)
Determine a field for the given constraint violation.The default implementation returns the stringified property path.
- Parameters:
violation
- the current JSR-303 ConstraintViolation- Returns:
- the Spring-reported field (for use with
Errors
) - Since:
- 4.2
- See Also:
ConstraintViolation.getPropertyPath()
,FieldError.getField()
determineErrorCode
protected String determineErrorCode(ConstraintDescriptor<?> descriptor)
Determine a Spring-reported error code for the given constraint descriptor.The default implementation returns the simple class name of the descriptor's annotation type. Note that the configured
MessageCodesResolver
will automatically generate error code variations which include the object name and the field name.- Parameters:
descriptor
- the JSR-303 ConstraintDescriptor for the current violation- Returns:
- a corresponding error code (for use with
Errors
) - Since:
- 4.2
- See Also:
ConstraintDescriptor.getAnnotation()
,MessageCodesResolver
getArgumentsForConstraint
protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor)
Return FieldError arguments for a validation error on the given field. Invoked for each violated constraint.The default implementation returns a first argument indicating the field name (see
getResolvableField(java.lang.String, java.lang.String)
). Afterwards, it adds all actual constraint annotation attributes (i.e. excluding "message", "groups" and "payload") in alphabetical order of their attribute names.Can be overridden to e.g. add further attributes from the constraint descriptor.
- Parameters:
objectName
- the name of the target objectfield
- the field that caused the binding errordescriptor
- the JSR-303 constraint descriptor- Returns:
- the Object array that represents the FieldError arguments
- See Also:
DefaultMessageSourceResolvable.getArguments()
,DefaultMessageSourceResolvable
,DefaultBindingErrorProcessor.getArgumentsForBindError(java.lang.String, java.lang.String)
getResolvableField
protected MessageSourceResolvable getResolvableField(String objectName, String field)
Build a resolvable wrapper for the specified field, allowing to resolve the field's name in aMessageSource
.The default implementation returns a first argument indicating the field: of type
DefaultMessageSourceResolvable
, with "objectName.field" and "field" as codes, and with the plain field name as default message.- Parameters:
objectName
- the name of the target objectfield
- the field that caused the binding error- Returns:
- a corresponding
MessageSourceResolvable
for the specified field - Since:
- 4.3
getRejectedValue
protected Object getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult)
Extract the rejected value behind the given constraint violation, for exposure through the Spring errors representation.- Parameters:
field
- the field that caused the binding errorviolation
- the corresponding JSR-303 ConstraintViolationbindingResult
- a Spring BindingResult for the backing object which contains the current field's value- Returns:
- the invalid value to expose as part of the field error
- Since:
- 4.2
- See Also:
ConstraintViolation.getInvalidValue()
,FieldError.getRejectedValue()
validate
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups)
validateProperty
public <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups)
- Specified by:
validateProperty
in interfaceValidator
validateValue
public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)
- Specified by:
validateValue
in interfaceValidator
getConstraintsForClass
public BeanDescriptor getConstraintsForClass(Class<?> clazz)
- Specified by:
getConstraintsForClass
in interfaceValidator