Annotation Type EnableWebMvc
@Retention(RUNTIME) @Target(TYPE) @Documented @Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc
Adding this annotation to an@Configurationclass imports the Spring MVC configuration fromWebMvcConfigurationSupport, e.g.:@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyWebConfiguration { }To customize the imported configuration, implement the interface
WebMvcConfigureror more likely extend the empty method base classWebMvcConfigurerAdapterand override individual methods, e.g.:@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyConfiguration extends WebMvcConfigurerAdapter { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } // More overridden methods ... }If
WebMvcConfigurerdoes not expose some advanced setting that needs to be configured, consider removing the@EnableWebMvcannotation and extending directly fromWebMvcConfigurationSupportorDelegatingWebMvcConfiguration, e.g.:@Configuration @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyConfiguration extends WebMvcConfigurationSupport { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { // Create or delegate to "super" to create and // customize properties of RequestMappingHandlerAdapter } }- Since:
- 3.1
- Author:
- Dave Syer, Rossen Stoyanchev
- See Also:
WebMvcConfigurer,WebMvcConfigurerAdapter,WebMvcConfigurationSupport,DelegatingWebMvcConfiguration