spring-framework / org.springframework.web.servlet.config.annotation / EnableWebMvc

EnableWebMvc

@Target([AnnotationTarget.CLASS, AnnotationTarget.FILE]) @Import(DelegatingWebMvcConfiguration) class EnableWebMvc

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport, e.g.:

 @Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration { } 

To customize the imported configuration, implement the interface WebMvcConfigurer and override individual methods, e.g.:

 @Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } } 

Note: only one @Configuration class may have the @EnableWebMvc annotation to import the Spring Web MVC configuration. There can however be multiple @Configuration classes implementing WebMvcConfigurer in order to customize the provided configuration.

If WebMvcConfigurer does not expose some more advanced setting that needs to be configured consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport or DelegatingWebMvcConfiguration, 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 } } 

Author
Dave Syer

Author
Rossen Stoyanchev

Since
3.1

See Also
org.springframework.web.servlet.config.annotation.WebMvcConfigurerorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupportorg.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration

Constructors

<init>

EnableWebMvc()

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport, e.g.:

 @Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration { } 

To customize the imported configuration, implement the interface WebMvcConfigurer and override individual methods, e.g.:

 @Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } } 

Note: only one @Configuration class may have the @EnableWebMvc annotation to import the Spring Web MVC configuration. There can however be multiple @Configuration classes implementing WebMvcConfigurer in order to customize the provided configuration.

If WebMvcConfigurer does not expose some more advanced setting that needs to be configured consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport or DelegatingWebMvcConfiguration, 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 } }