Spring Boot - Registering a custom ConfigurableWebBindingInitializer as a Bean

[Updated: Dec 30, 2017, Created: Dec 29, 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 {
      SpringApplication.run(ExampleMain.class, 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-starter-web 1.5.9.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container [Description from spring-boot-starter-web-1.5.9.RELEASE.pom]
    Corresponding Spring version: 4.3.13.RELEASE
  • 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