WebDataBinder is the Spring MVC class that copies request parameters onto Java objects, converting types and collecting binding errors along the way. When a controller method takes a @ModelAttribute object (or a plain form object), Spring creates a WebDataBinder for it, binds the request values, and runs validation. You customise that process in an @InitBinder method.
It is a subclass of DataBinder with web-specific extras, such as handling of checkbox markers and file parts. It is not used for @RequestBody JSON, which is read by an HttpMessageConverter such as Jackson; validation of such bodies still happens through a binder, but binding itself does not.
For every handler argument that needs binding, Spring MVC follows these steps:
WebDataBinder for the target object, with the registered conversion service and validators.@InitBinder methods that apply to the controller.@Valid or @Validated is present.BindingResult, which you can declare right after the argument.If you do not declare a BindingResult, a binding or validation failure throws MethodArgumentNotValidException or BindException, which Spring turns into a 400 response by default.
Parameter names are matched to property names, including nested ones such as address.city.
Since Spring Framework 6.1, records and classes with a single constructor are bound through constructor binding, so immutable types work without setters. A value that cannot be converted, for example age=abc, becomes a field error in the BindingResult rather than an exception.
An @InitBinder method receives the WebDataBinder before binding starts. It is the place to register formatters, editors and validators.
PropertyEditor instances are the older mechanism and are not thread-safe, so they are created per binder. Prefer Formatter or Converter registered globally through WebMvcConfigurer.addFormatters when the rule applies to the whole application. An @InitBinder method in a @ControllerAdvice class applies to every controller, and @InitBinder("order") limits it to one model attribute name.
Binding every request parameter onto an object is a security risk if the object has fields the user must never set, such as role or price. Mass assignment is a real vulnerability class, and WebDataBinder gives you controls against it.
The strongest approach is to bind to a dedicated form or DTO type that contains only the editable fields, and then map it to your entity. Since 6.1, binder.setDeclarativeBinding(true) makes the binder bind only the fields you declare as allowed, which is a safer default for sensitive models.
You can attach Spring Validator implementations next to Bean Validation constraints.
Use addValidators to keep the default validator, and setValidator only when you want to replace it. For HTML checkboxes, the binder understands the _fieldName marker parameter, which lets it tell "unchecked" from "not submitted". You can change the marker prefix with setFieldMarkerPrefix and the default-value prefix with setFieldDefaultPrefix.
You can create a binder yourself, which is useful in tests or when binding map data.
No. @RequestBody uses an HttpMessageConverter, usually Jackson. The binder handles query parameters, form fields and multipart data.
DataBinder is the generic binding class in spring-context. WebDataBinder adds web features such as field markers and multipart file binding.
Yes. Declare an @InitBinder method in that controller and register the formatter or editor on the binder.
Call setDisallowedFields or setAllowedFields on the binder, or better, bind to a DTO that lacks that field.
WebDataBinder turns request parameters into typed objects, records conversion and validation errors in a BindingResult, and is configured with @InitBinder. Register formatters and validators there, restrict bindable fields to avoid mass assignment, and remember it handles form and query data, not JSON bodies.