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


@Retention(value=RUNTIME)
@Target(value=TYPE)
@Documented
@Import(value=DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc

Enables default Spring MVC configuration and registers Spring MVC infrastructure components expected by the DispatcherServlet. Use this annotation on an @Configuration class. In turn that will import DelegatingWebMvcConfiguration, which provides default Spring MVC configuration.

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

 }
 

To customize the imported configuration implement WebMvcConfigurer, or more conveniently extend WebMvcConfigurerAdapter overriding specific methods only. Any @Configuration class that implements WebMvcConfigurer will be detected by DelegatingWebMvcConfiguration and given an opportunity to customize the default Spring MVC code-based configuration.

 @Configuration
 @EnableWebMvc
 @ComponentScan(
        basePackageClasses = { MyConfiguration.class },
        excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.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());
        }

        // @Override methods ...

 }
 

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