Annotation Interface EnablewebMvc
@Retention(RUNTIME)
@Target(TYPE)
@Documented
@Import(DelegatingwebMvcConfiguration.class)
public @interface EnablewebMvc
Adding this annotation to an
@Configuration class imports the Spring MVC
configuration from webMvcConfigurationSupport, for example:
@Configuration
@EnablewebMvc
@ComponentScan(basePackageClasses = MyConfiguration.class)
public class MyConfiguration {
}
To customize the imported configuration, implement the interface
webMvcConfigurer and override individual methods, for example:
@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, for example:
@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: