@EnableWebMvchtml
Referencejava
The above registers a RequestMappingHandlerMapping
, a RequestMappingHandlerAdapter
, and anExceptionHandlerExceptionResolver
(among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping
, @ExceptionHandler
, and others.web
It also enables the following:spring
@NumberFormat
annotation through theConversionService
.@DateTimeFormat
annotation.@Valid
, if a JSR-303 Provider is present on the classpath.HttpMessageConverter support for @RequestBody
method parameters and @ResponseBody
method return values from @RequestMapping
or @ExceptionHandler
methods.api
This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:mvc
ByteArrayHttpMessageConverter
converts byte arrays.StringHttpMessageConverter
converts strings.ResourceHttpMessageConverter
converts to/from org.springframework.core.io.Resource
for all media types.SourceHttpMessageConverter
converts to/from a javax.xml.transform.Source
.FormHttpMessageConverter
converts form data to/from a MultiValueMap<String, String>
.Jaxb2RootElementHttpMessageConverter
converts Java objects to/from XML — added if JAXB2 is present on the classpath.MappingJackson2HttpMessageConverter
(or MappingJacksonHttpMessageConverter
) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.AtomFeedHttpMessageConverter
converts Atom feeds — added if Rome is present on the classpath.RssChannelHttpMessageConverter
converts RSS feeds — added if Rome is present on the classpath.DOCapp
Add this annotation to an @Configuration
class to have the Spring MVC configuration defined in WebMvcConfigurationSupport
imported:ide
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = { MyConfiguration.class })
public class MyWebConfiguration {
}
Customize the imported configuration by implementing the WebMvcConfigurer
interface or more likely by extending theWebMvcConfigurerAdapter
base class and overriding individual methods:this
@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:spa
@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
}
}
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
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 HandlerMapping
s:
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 HandlerAdapter
s:
RequestMappingHandlerAdapter
for processing requests with annotated controller methods.HttpRequestHandlerAdapter
for processing requests with HttpRequestHandler
s.SimpleControllerHandlerAdapter
for processing requests with interface-based Controller
s.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 typesBoth the RequestMappingHandlerAdapter
and the ExceptionHandlerExceptionResolver
are configured with default instances of the following by default:
ContentNegotiationManager
DefaultFormattingConversionService
LocalValidatorFactoryBean
if a JSR-303 implementation is available on the classpathHttpMessageConverter
s depending on the 3rd party libraries available on the classpath.org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
WebMvcConfigurationSupport
that detects and delegates to all beans of type WebMvcConfigurer
allowing them to customize the configuration provided by WebMvcConfigurationSupport
. This is the class actually imported by @EnableWebMvc
.
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
Defines callback methods to customize the Java-based configuration for Spring MVC enabled via @EnableWebMvc
.
@EnableWebMvc
-annotated configuration classes may implement this interface to be called back and given a chance to customize the default configuration. Consider extending WebMvcConfigurerAdapter
, which provides a stub implementation of all interface methods.
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
An implementation of WebMvcConfigurer
with empty methods allowing sub-classes to override only the methods they're interested in.
搞清這四個類的關係。