org.springframework.web.servlet.config.annotation
Annotation Type EnableWebMvc


@Retention(value=RUNTIME)
@Target(value=TYPE)
@Documented
@Import(value=org.springframework.web.servlet.config.annotation.WebMvcConfiguration.class)
public @interface EnableWebMvc

Enables default Spring MVC configuration and registers Spring MVC infrastructure components expected by the DispatcherServlet. To use this annotation simply place it on an application @Configuration class and that will in turn import default Spring MVC configuration including support for annotated methods in @Controller classes.

 @Configuration
 @EnableWebMvc
 @ComponentScan(
        basePackageClasses = { MyConfiguration.class },
        excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) }
 )
 public class MyConfiguration {

 }
 

To customize the imported configuration you simply implement WebMvcConfigurer, or more likely extend WebMvcConfigurerAdapter overriding selected methods only. The most obvious place to do this is the @Configuration class that enabled the Spring MVC configuration via @EnableWebMvc. However any @Configuration class and more generally any Spring bean can implement WebMvcConfigurer to be detected and given an opportunity to customize Spring MVC configuration at startup.

 @Configuration
 @EnableWebMvc
 @ComponentScan(
        basePackageClasses = { MyConfiguration.class },
        excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) }
 )
 public class MyConfiguration extends WebMvcConfigurerAdapter {

        @Override
        public void registerFormatters(FormatterRegistry formatterRegistry) {
                formatterRegistry.addConverter(new MyConverter());
        }

        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new MyHttpMessageConverter());
        }

        ...

 }
 

Since:
3.1
Author:
Dave Syer, Rossen Stoyanchev
See Also:
WebMvcConfigurer, WebMvcConfigurerAdapter