The @InitBinder annotation marks a controller method that customises how Spring MVC binds request parameters to objects. Spring calls it before binding for each request, passing a WebDataBinder that you can configure with custom editors, formatters, validators, and allowed or disallowed fields.
Use it for rules that apply to a controller's binding, such as parsing a date format, trimming strings, or restricting which fields a form may set. On modern Spring (6.x, Spring Boot 3.x) it works the same in @Controller, @RestController and @ControllerAdvice classes.
When a request reaches a handler method, Spring MVC converts request parameters, path variables and form fields into method arguments and @ModelAttribute objects. A WebDataBinder performs that work. Before it does, Spring invokes every @InitBinder method that applies to the handler, so the method can change the binder's configuration for that request.
An @InitBinder method must return void. Its arguments can include the WebDataBinder plus other supported arguments such as Locale or @PathVariable values.
The most common use is to control how a string is converted to a type. This example trims whitespace and turns empty strings into null for every String field, and parses dates in a fixed format.
StringTrimmerEditor(true) converts blank input to null, which makes @NotNull validation behave predictably. PropertyEditor implementations are not thread-safe, which is why Spring asks you to register a new instance on each binder instead of sharing one.
For dates and numbers, the modern approach is a Formatter or an annotation such as @DateTimeFormat, which work with java.time types.
If you need a controller-specific rule, add a formatter to the binder:
For a rule that applies to the whole application, register a Formatter through WebMvcConfigurer.addFormatters instead of repeating @InitBinder in every controller.
Binding a request straight into an object can let a client set fields you did not intend, such as role or price. This is the mass assignment problem. @InitBinder can set an allow list.
You can also use setDisallowedFields, but an allow list is safer because new fields stay blocked by default. A dedicated form object or DTO that contains only the permitted fields is an even better design, and the two techniques work together.
@InitBinder can attach a Validator that runs when a model attribute is annotated with @Valid or @Validated.
addValidators keeps the validators that Spring configured, such as the Bean Validation validator, while setValidator replaces them. Prefer addValidators unless you intend to replace the default.
Pass one or more names to the annotation to apply the method only to matching attributes or request parameters.
Without a name, the method applies to every binding in that controller. Name matching uses the model attribute name, which defaults to the class name with a lowercase first letter.
Place @InitBinder methods in a class annotated with @ControllerAdvice to apply them to many controllers.
You can narrow the scope with attributes such as basePackages or assignableTypes on @ControllerAdvice.
@RequestBody JSON payloads. Jackson handles those through HttpMessageConverter, so use Jackson modules or annotations there.@ModelAttribute, form data, query parameters and path variables.@InitBinder called once or on every request?On every request that reaches a matching handler, because a new WebDataBinder is created each time. Keep the method light.
@InitBinder work with @RestController?Yes, but it only affects parameter binding such as query strings and path variables. It does not change how a JSON request body is parsed.
PropertyEditor or Formatter?Prefer Formatter or Converter for new code. They are stateless, thread-safe and work with java.time. PropertyEditor remains useful for existing code and for StringTrimmerEditor.
@InitBinder method return a value?No. It must return void, and Spring ignores anything else.
@InitBinder configures the WebDataBinder before Spring MVC binds request data to objects. Use it to register formatters and editors, restrict bindable fields, add validators, and scope the rules to a single model attribute. Move rules shared by many controllers into @ControllerAdvice or WebMvcConfigurer.