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

Package org.springframework.web.servlet.config.annotation

Types

CorsRegistration

open class CorsRegistration

Assists with the creation of a CorsConfiguration instance mapped to a path pattern. By default all origins, headers, and credentials for GET, HEAD, and POST requests are allowed while the max age is set to 30 minutes.

InterceptorRegistration

open class InterceptorRegistration

Assists with the creation of a MappedInterceptor.

RedirectViewControllerRegistration

open class RedirectViewControllerRegistration

Assist with the registration of a single redirect view controller.

ResourceHandlerRegistration

open class ResourceHandlerRegistration

Encapsulates information required to create a resource handler.

UrlBasedViewResolverRegistration

open class UrlBasedViewResolverRegistration

Assist with configuring a org.springframework.web.servlet.view.UrlBasedViewResolver.

ViewControllerRegistration

open class ViewControllerRegistration

Assist with the registration of a single view controller.

WebMvcConfigurationSupport

open class WebMvcConfigurationSupport : ApplicationContextAware, ServletContextAware

This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding EnableWebMvc to an application Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary, remembering to add Configuration to the subclass and Bean to overridden Bean methods. For more details see the javadoc of EnableWebMvc.

This class registers the following HandlerMappings:

  • RequestMappingHandlerMapping ordered at 0 for mapping requests to annotated controller methods.
  • HandlerMapping ordered at 1 to map URL paths directly to view names.
  • BeanNameUrlHandlerMapping ordered at 2 to map URL paths to controller bean names.
  • HandlerMapping ordered at Integer.MAX_VALUE-1 to serve static resource requests.
  • HandlerMapping ordered at Integer.MAX_VALUE to forward requests to the default servlet.

Registers these HandlerAdapters:

  • RequestMappingHandlerAdapter for processing requests with annotated controller methods.
  • HttpRequestHandlerAdapter for processing requests with HttpRequestHandlers.
  • SimpleControllerHandlerAdapter for processing requests with interface-based Controllers.

Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

  • ExceptionHandlerExceptionResolver for handling exceptions through @ExceptionHandler methods.
  • ResponseStatusExceptionResolver for exceptions annotated with @ResponseStatus.
  • DefaultHandlerExceptionResolver for resolving known Spring exception types

Registers an AntPathMatcher and a UrlPathHelper to be used by:

  • the RequestMappingHandlerMapping,
  • the HandlerMapping for ViewControllers
  • and the HandlerMapping for serving resources
Note that those beans can be configured with a PathMatchConfigurer.

Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:

  • a ContentNegotiationManager
  • a DefaultFormattingConversionService
  • a org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean if a JSR-303 implementation is available on the classpath
  • a range of HttpMessageConverters depending on the third-party libraries available on the classpath.

WebMvcConfigurerAdapter

abstract class WebMvcConfigurerAdapter : WebMvcConfigurer

An implementation of WebMvcConfigurer with empty methods allowing subclasses to override only the methods they're interested in.

Annotations

EnableWebMvc

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