The @InitBinder annotation marks a controller method that configures the WebDataBinder used to bind request parameters to method arguments in Spring MVC. Inside it you register custom formatters, property editors, validators, or restrict which fields can be bound. It runs before binding for each request handled by that controller.
Typical uses are parsing dates and trimming strings, attaching an extra Validator, and limiting bindable fields for security. The same method can sit in a @ControllerAdvice class to apply to every controller.
Before a handler method receives a @ModelAttribute object, a request parameter, or a path variable, Spring creates a WebDataBinder to convert the strings in the request into the target type. Methods annotated with @InitBinder are called first, so you can adjust that binder.
An @InitBinder method must return void. Its arguments can be the WebDataBinder plus the usual request-related types such as WebRequest or Locale.
The old approach registered a PropertyEditor, which is not thread-safe if it wraps SimpleDateFormat. Prefer java.time types with a formatter or the declarative @DateTimeFormat annotation.
The simplest modern option needs no binder at all:
When you need a global rule for one controller, register a property editor that creates a new formatter per call:
StringTrimmerEditor(true) trims whitespace and converts empty strings to null, which is a common cleanup for form fields. Since Spring 3, Formatter and Converter beans registered through WebMvcConfigurer.addFormatters are the preferred way to apply a rule globally.
You can attach a Validator to the binder so it runs alongside bean validation.
Passing a name, as in @InitBinder("user"), limits the method to the model attribute or parameter called user. Without a name it applies to all of them.
To prevent mass assignment, where a client posts fields you did not intend to expose, allow only specific fields:
You can also use setDisallowedFields. A cleaner defence is to bind to a dedicated form object or record that contains only the safe fields.
Put the method in a class annotated with @ControllerAdvice to share it.
No. @RequestBody uses HttpMessageConverter instances such as Jackson, not the WebDataBinder. Use Jackson annotations or a custom deserializer for JSON.
It is invoked each time a binder is created for a handler's arguments, so it runs on every request handled by that controller, before the handler method.
Prefer Formatter, Converter or @DateTimeFormat. They are thread-safe and work for both MVC and other parts of Spring.
Methods in @ControllerAdvice classes run before those in the controller itself; within a class do not rely on order.
@InitBinder customises the WebDataBinder for a controller: register converters, trim input, add validators, or whitelist bindable fields. Use @InitBinder("name") to target one attribute, @ControllerAdvice to share it, and prefer thread-safe formatters over SimpleDateFormat-based editors. It does not affect JSON bodies bound with @RequestBody.