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 .
Example
Spring MVC Controller
@Controller
@RequestMapping("/")
public class TradeController {
@GetMapping("/trade")
@ResponseBody
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:
@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);
}
}
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.
Output
Example ProjectDependencies 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
|