Spring MVC - ConfigurableWebBindingInitializer Examples

Code Snippets Spring MVC 

@Configuration
@ComponentScan
public class MyWebConfig extends WebMvcConfigurationSupport {

@Override
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
ConfigurableWebBindingInitializer initializer = super.getConfigurableWebBindingInitializer();
initializer.setPropertyEditorRegistrar(propertyEditorRegistry -> {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
propertyEditorRegistry.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormatter, true));
});
return initializer;
}
}
Original Post




Using ConfigurableWebBindingInitializer in Spring Boot:

@SpringBootApplication
public class ExampleMain {

@Bean
public ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
FormattingConversionService conversionService = new DefaultFormattingConversionService();
//we can add our custom converters and formatters
//conversionService.addConverter(...);
//conversionService.addFormatter(...);
initializer.setConversionService(conversionService);
//we can set our custom validator
//initializer.setValidator(....);

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

public static void main(String[] args) throws InterruptedException {
SpringApplication.run(ExampleMain.class, args);
}
}
Original Post




See Also