The @InitBinder annotation in Spring MVC marks a method that configures the WebDataBinder used to convert request parameters, path variables and form fields into Java types. You use it to register converters, formatters or property editors for a controller, and to set rules for which fields can be bound.
It is the hook for local binding rules. Use it when a type needs special parsing in one controller, and use global converters when the rule applies to the whole application.
Before a handler method runs, Spring MVC creates a WebDataBinder for each argument that needs conversion. It then invokes all @InitBinder methods that apply: those in the controller itself and those in any matching @ControllerAdvice class. The binder is created per request, so registering an editor on it is safe even though editors are stateful.
The method returns void. Besides the binder, it can accept arguments such as Locale or WebRequest.
By default an @InitBinder method applies to all attributes and parameters of the controller. Give it names and it only runs for those.
Here LocalDateEditor stands for your own PropertyEditor implementation. The name matches a model attribute name or a request parameter name. This lets one controller use different formats for different parameters without conflicts.
The example below parses a Status enum from a query parameter in a case-insensitive way. The binder, not the handler, does the conversion.
A request to /orders?status=active returns Listing ACTIVE orders. An unknown value makes valueOf throw an IllegalArgumentException, which Spring reports as a type mismatch and returns as a 400 response.
Spring has three conversion mechanisms. @InitBinder can register all of them on a binder.
| Mechanism | Registered with | Thread safety | Best for |
|---|---|---|---|
PropertyEditor | registerCustomEditor | Not thread-safe, new instance per binder | Legacy code, String to object |
Formatter | addCustomFormatter | Thread-safe | Locale-aware text such as dates and currency |
Converter | ConversionService | Thread-safe | Type to type, any source |
For new code, prefer converters and formatters. Register application-wide ones through WebMvcConfigurer.addFormatters and keep @InitBinder for rules specific to a controller.
Put the method in a @ControllerAdvice class when several controllers need the same rule, for example trimming every string.
You can narrow the advice with @ControllerAdvice(basePackages = "com.example.admin") when only some controllers should be affected.
@InitBinder does not change how a @RequestBody is read. JSON payloads go through an HttpMessageConverter such as Jackson, so configure Jackson for those. Also, the binder is not used for values you read yourself from HttpServletRequest.
For security, call binder.setAllowedFields(...) or setDisallowedFields(...) on form-backed objects so a client cannot set properties you never meant to expose.
No. The method must return void. It changes the binder it receives.
No. It affects any binding done by WebDataBinder, including @RequestParam, @PathVariable, @ModelAttribute and @RequestHeader values that need type conversion.
@DateTimeFormat is an annotation on a field or parameter that sets a date pattern declaratively. @InitBinder is code on the controller, which is more flexible but more verbose.
No. Spring Boot auto-configures Spring MVC, and @InitBinder methods are detected the same way as in a plain Spring application.
@InitBinder customizes the WebDataBinder for a controller or, through @ControllerAdvice, for many controllers. Name the target attribute to scope it, register formatters in preference to property editors, and restrict bindable fields for safety. It does not apply to JSON request bodies.