Spring Framework

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


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

Add this annotation to an @Configuration class to have the Spring MVC configuration defined in WebMvcConfigurationSupport imported:

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

 }
 

Customize the imported configuration by implementing the WebMvcConfigurer interface or more likely by extending the WebMvcConfigurerAdapter base class and overriding individual methods:

 @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 the customization options of WebMvcConfigurer do not expose something you need to configure, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport overriding selected @Bean methods:

 @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 RequestMapingHandlerAdapter
        }
 }
 

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


Spring Framework