Class DataBinder

All Implemented Interfaces:
PropertyEditorRegistry, TypeConverter
Direct Known Subclasses:

public class DataBinder extends Object implements PropertyEditorRegistry, TypeConverter
Binder that allows applying property values to a target object via constructor and setter injection, and also supports validation and binding result analysis.

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:

  • Missing field error: "required"
  • Type mismatch error: "typeMismatch"
  • Method invocation error: "methodInvocation"

By default, binding errors get resolved through the BindingErrorProcessor strategy, processing for missing fields and property access exceptions: see the setBindingErrorProcessor(org.springframework.validation.BindingErrorProcessor) 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 MessageSource, which is able to resolve an ObjectError/FieldError through its MessageSource.getMessage(org.springframework.context.MessageSourceResolvable, java.util.Locale) method. The list of message codes can be customized through the MessageCodesResolver strategy: see the setMessageCodesResolver(org.springframework.validation.MessageCodesResolver) method. DefaultMessageCodesResolver's javadoc states details on the default resolution rules.

This generic data binder can be used in any kind of environment.

Rod Johnson, Juergen Hoeller, Rob Harrop, Stephane Nicoll, Kazuki Shimizu, Sam Brannen
See Also:
  • Field Details


      public static final String DEFAULT_OBJECT_NAME
      Default object name used for binding: "target".
      See Also:

      public static final int DEFAULT_AUTO_GROW_COLLECTION_LIMIT
      Default limit for array and collection growing: 256.
      See Also:
    • logger

      protected static final Log logger
      We'll create a lot of DataBinder instances: Let's use a static logger.
  • Constructor Details

    • DataBinder

      public DataBinder(@Nullable Object target)
      Create a new DataBinder instance, with default object name.
      target - the target object to bind onto (or null if the binder is just used to convert a plain parameter value)
      See Also:
    • DataBinder

      public DataBinder(@Nullable Object target, String objectName)
      Create a new DataBinder instance.
      target - the target object to bind onto (or null if the binder is just used to convert a plain parameter value)
      objectName - the name of the target object
  • Method Details