To enable MVC Java config add the annotation @EnableWebMvc
to one of your @Configuration
classes:
@Configuration @EnableWebMvc public class WebConfig { }
To achieve the same in XML use the mvc:annotation-driven
element in your DispatcherServlet context (or in your root context if you have no DispatcherServlet context defined):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> </beans>
The above registers a RequestMappingHandlerMapping
, a RequestMappingHandlerAdapter
, and an ExceptionHandlerExceptionResolver
(among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping
, @ExceptionHandler
, and others.
It also enables the following:
- Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.
- Support for formatting Number fields using the
@NumberFormat
annotation through theConversionService
. - Support for formatting
Date
,Calendar
,Long
, and Joda Time fields using the@DateTimeFormat
annotation. - Support for validating
@Controller
inputs with@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.This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:
ByteArrayHttpMessageConverter
converts byte arrays.StringHttpMessageConverter
converts strings.ResourceHttpMessageConverter
converts to/fromorg.springframework.core.io.Resource
for all media types.SourceHttpMessageConverter
converts to/from ajavax.xml.transform.Source
.FormHttpMessageConverter
converts form data to/from aMultiValueMap<String, String>
.Jaxb2RootElementHttpMessageConverter
converts Java objects to/from XML — added if JAXB2 is present and Jackson 2 XML extension is not present on the classpath.MappingJackson2HttpMessageConverter
converts to/from JSON — added if Jackson 2 is present on the classpath.MappingJackson2XmlHttpMessageConverter
converts to/from XML — added if Jackson 2 XML extension 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.
See Section 22.16.12, “Message Converters” for more information about how to customize these default converters.
![]() |
Jackson JSON and XML converters are created using This builder customizes Jackson’s default properties with the following ones:
It also automatically registers the following well-known modules if they are detected on the classpath:
|