The binding process can be customized by specifying allowed field patterns, required fields, custom editors, etc.
Warning
Data binding can lead to security issues by exposing parts of the object graph that are not meant to be accessed or modified by external clients. Therefore, the design and use of data binding should be considered carefully with regard to security. For more details, please refer to the dedicated sections on data binding for Spring Web MVC and Spring WebFlux in the reference manual.
The binding results can be examined via the BindingResult interface,
extending the Errors interface: see the getBindingResult() method.
Missing fields and property access exceptions will be converted to FieldErrors,
collected in the Errors instance, using the following error codes:
By default, binding errors get resolved through the BindingErrorProcessor
strategy, processing for missing fields and property access exceptions: see the
setBindingErrorProcessor method. You can override the default strategy
if needed, for example to generate different error codes.
Custom validation errors can be added afterwards. You will typically want to resolve
such error codes into proper user-visible error messages; this can be achieved through
resolving each error via a org., which is
able to resolve an ObjectError/FieldError through its
org.
method. The list of message codes can be customized through the MessageCodesResolver
strategy: see the setMessageCodesResolver method. DefaultMessageCodesResolver's
javadoc states details on the default resolution rules.
This generic data binder can be used in any kind of environment.
setAllowedFields, setRequiredFields, registerCustomEditor, setMessageCodesResolver, setBindingErrorProcessor, construct, bind, getBindingResult, DefaultMessageCodesResolver, DefaultBindingErrorProcessor, org.springframework.context.MessageSource
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| public static interface | DataBinder.NameResolver
Strategy to determine the name of the value to bind to a method parameter. |
| public static interface | DataBinder.ValueResolver
Strategy for |
| Modifier and Type | Field and Description |
|---|---|
| private String[] | |
| private int | |
| private boolean | |
| private BindingErrorProcessor | |
| private AbstractPropertyBindingResult | |
| private ConversionService | |
| private boolean | |
| public static final int | DEFAULT_AUTO_GROW_COLLECTION_LIMIT
Default limit for array and collection growing: 256. |
| public static final String | DEFAULT_OBJECT_NAME
Default object name used for binding: "target". |
| private boolean | |
| private String[] | |
| private Predicate | |
| private boolean | |
| private boolean | |
| protected static final Log | logger
We'll create a lot of DataBinder instances: Let's use a static logger. |
| private MessageCodesResolver | |
| private DataBinder. | |
| private final String | |
| private String[] | |
| private Object | |
| pack-priv ResolvableType | |
| private DataBinder. | |
| private final List |
| Access | Constructor and Description |
|---|---|
| public | DataBinder(Object
the target object to bind onto (or target)null
if the binder is just used to convert a plain parameter value)Create a new DataBinder instance, with default object name. |
| public | DataBinder(Object
the target object to bind onto (or target, String null
if the binder is just used to convert a plain parameter value)the name of the target object objectName)Create a new DataBinder instance. |
| Modifier and Type | Method and Description |
|---|---|
| public void | addCustomFormatter(Formatter<?>
the formatter to add, generically declared for a specific type formatter)Add a custom formatter, applying it to all fields matching the
|
| public void | addCustomFormatter(Formatter<?>
the formatter to add, generically declared for a specific type formatter, String... the fields to apply the formatter to, or none if to be applied to all fields)Add a custom formatter for the field type specified in |
| public void | addCustomFormatter(Formatter<?>
the formatter to add (does not need to generically declare a
field type if field types are explicitly specified as parameters) formatter, Class<?>... the field types to apply the formatter to, or none if to be
derived from the given fieldTypes)Formatter implementation classAdd a custom formatter, applying it to the specified field types only, if any,
or otherwise to all fields matching the |
| public void | |
| protected void | applyPropertyValues(MutablePropertyValues
the property values to be bound (can be modified) mpvs)Apply given property values to the target object. |
| private void | |
| public void | bind(PropertyValues
property values to bind pvs)Bind the given property values to this binder's target. |
| protected void | checkAllowedFields(MutablePropertyValues
the property values to be bound (can be modified) mpvs)Check the given property values against the allowed fields, removing values for fields that are not allowed. |
| protected void | checkRequiredFields(MutablePropertyValues
the property values to be bound (can be modified) mpvs)Check the given property values against the required fields, generating missing field errors where appropriate. |
| public Map | Returns: the model Map, containing target object and Errors instanceClose this DataBinder, which may result in throwing a BindException if it encountered any errors. |
| public void | construct(DataBinder.
to resolve constructor argument values with valueResolverCreate the target with constructor injection of values. |
| public <T> T | convertIfNecessary(Object
the value to convert value, Class<T> the type we must convert to
(or requiredType)null if not known, for example in case of a collection element)Implements org. |
| public <T> T | convertIfNecessary(Object
the value to convert value, Class<T> the type we must convert to
(or requiredType, MethodParameter null if not known, for example in case of a collection element)the method parameter that is the target of the conversion
(for analysis of generic types; may be methodParam)null)Implements org. |
| public <T> T | convertIfNecessary(Object
the value to convert value, Class<T> the type we must convert to
(or requiredType, Field null if not known, for example in case of a collection element)the reflective field that is the target of the conversion
(for analysis of generic types; may be field)null)Implements org. |
| public <T> T | convertIfNecessary(Object
the value to convert value, Class<T> the type we must convert to
(or requiredType, TypeDescriptor null if not known, for example in case of a collection element)the type descriptor to use (may be typeDescriptor)null))Overrides default org. |
| protected AbstractPropertyBindingResult | createBeanPropertyBindingResult()
Create the |
| protected AbstractPropertyBindingResult | createDirectFieldBindingResult()
Create the |
| private Object | |
| protected void | doBind(MutablePropertyValues
the property values to bind,
as MutablePropertyValues instance mpvs)Actual implementation of the binding process, working with the passed-in MutablePropertyValues instance. |
| public PropertyEditor | findCustomEditor(Class<?>
the type of the property (can be requiredType, String null if a property
is given but should be specified in any case for consistency checking)the path of the property (name or nested path), or
propertyPath)null if looking for an editor for all properties of the given typeImplements org. |
| public String[] | Returns: array of allowed field patternsReturn the field patterns that should be allowed for binding. |
| public int | |
| public BindingErrorProcessor | |
| public BindingResult | Returns: the BindingResult instance, to be treated as BindingResult or as Errors instance (Errors is a super-interface of BindingResult)Return the BindingResult instance created by this DataBinder. |
| public ConversionService | |
| public String[] | Returns: array of disallowed field patternsReturn the field patterns that should not be allowed for binding. |
| protected AbstractPropertyBindingResult | getInternalBindingResult()
Return the internal BindingResult held by this DataBinder, as an AbstractPropertyBindingResult. |
| public DataBinder. | |
| public String | |
| protected ConfigurablePropertyAccessor | |
| protected PropertyEditorRegistry | |
| public String[] | Returns: array of field namesReturn the fields that are required for each binding process. |
| protected SimpleTypeConverter | |
| public Object | |
| public ResolvableType | |
| protected TypeConverter | |
| public Validator | |
| public List | |
| public List | |
| private boolean | |
| public void | |
| public void | initDirectFieldAccess()
Initialize direct field access for this DataBinder, as alternative to the default bean property access. |
| protected boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| public void | registerCustomEditor(Class<?>
the type of the property requiredType, PropertyEditor the editor to register propertyEditor)Implements org. |
| public void | registerCustomEditor(Class<?>
the type of the property. This may be requiredType, String null
if a property is given but should be specified in any case, in particular in
case of a Collection - making clear whether the editor is supposed to apply
to the entire Collection itself or to each of its entries. So as a general rule:
Do not specify null here in case of a Collection/array!the path of the property (name or nested path), or
field, PropertyEditor null if registering an editor for all properties of the given typeeditor to register propertyEditor)Implements org. |
| public void | |
| public void | setAllowedFields(String...
array of allowed field patterns allowedFields)Register field patterns that should be allowed for binding. |
| public void | setAutoGrowCollectionLimit(int autoGrowCollectionLimit)
Specify the limit for array and collection auto-growing. |
| public void | setAutoGrowNestedPaths(boolean autoGrowNestedPaths)
Set whether this binder should attempt to "auto-grow" a nested path that contains a null value. |
| public void | setBindingErrorProcessor(BindingErrorProcessor bindingErrorProcessor)
Set the strategy to use for processing binding errors, that is,
required field errors and |
| public void | setConversionService(ConversionService conversionService)
Specify a |
| public void | setDeclarativeBinding(boolean declarativeBinding)
Set whether to bind only fields explicitly intended for binding including:
|
| public void | setDisallowedFields(String...
array of disallowed field patterns disallowedFields)Register field patterns that should not be allowed for binding. |
| public void | |
| public void | setIgnoreInvalidFields(boolean ignoreInvalidFields)
Set whether to ignore invalid fields, that is, whether to ignore bind parameters that have corresponding fields in the target object which are not accessible (for example because of null values in the nested path). |
| public void | setIgnoreUnknownFields(boolean ignoreUnknownFields)
Set whether to ignore unknown fields, that is, whether to ignore bind parameters that do not have corresponding fields in the target object. |
| public void | setMessageCodesResolver(MessageCodesResolver messageCodesResolver)
Set the strategy to use for resolving errors into message codes. |
| public void | setNameResolver(DataBinder.
the resolver to use nameResolverConfigure a resolver to determine the name of the value to bind to a
constructor parameter in |
| public void | setRequiredFields(String...
array of field names requiredFields)Register fields that are required for each binding process. |
| public void | setTargetType(ResolvableType
the type of the target object targetType)Set the type for the target object. |
| public void | |
| protected boolean | shouldConstructArgument(MethodParameter param)
Whether to instantiate the constructor argument of the given type, matching its own constructor arguments to bind values. |
| protected boolean | |
| public void | |
| public void | validate(Object...
one or more hint objects to be passed to a validationHints)SmartValidatorInvoke the specified Validators, if any, with the given validation hints. |
| private void | validateConstructorArgument(Class<?> constructorClass, String nestedPath, String name, Object value)
|