Spring Boot - Registering a custom ConfigurableWebBindingInitializer as a Bean

[Last Updated: Dec 30, 2017]

In Spring MVC, we can use a custom ConfigurableWebBindingInitializer by directly extending WebMvcConfigurationSupport (example here).

In Spring Boot application, we just need to register custom ConfigurableWebBindingInitializer as a bean which will replace the default one. If curious then you can see how that happens in getConfigurableWebBindingInitializer() method's source code of EnableWebMvcConfiguration which is a nested class of WebMvcAutoConfiguration.


Spring MVC Controller

public class TradeController {

  public String handleRequest(@RequestParam Date tradeDate) {
      return "request received for " + tradeDate;

Java Config and Spring boot main class

In following example, we are adding back the DefaultFormattingConversionService so that the default conversion should work as it is. We are additionally adding a custom PropertyEditor. Also see the comments for additional configuration we can do:

public class ExampleMain {

  public ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
      ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
      FormattingConversionService conversionService = new DefaultFormattingConversionService();
      //we can add our custom converters and formatters
      //we can set our custom validator

      //here we are setting a custom PropertyEditor
      initializer.setPropertyEditorRegistrar(propertyEditorRegistry -> {
          SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
                  new CustomDateEditor(dateFormatter, true));
      return initializer;

  public static void main(String[] args) throws InterruptedException {, args);

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

We can also run the main class from our IDE.


Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.5.9.RELEASE
    Corresponding Spring Version 4.3.13.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • JDK 1.8
  • Maven 3.3.9

Spring Boot - Custom ConfigurableWebBindingInitializer Example Select All Download
  • configurable-web-binding-initializer-example
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also