您的位置:首页 > 编程语言 > Java开发

spring mvc <mvc:annotation-driver/>详解

2016-06-12 18:39 585 查看
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
,
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 the 
ConversionService
.
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/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 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 
ObjectMapper
 instances
created by 
Jackson2ObjectMapperBuilder
 in
order to provide a better default configuration.

This builder customizes Jackson’s default properties with the following ones:

DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
 is
disabled.

MapperFeature.DEFAULT_VIEW_INCLUSION
 is
disabled.

It also automatically registers the following well-known modules if they are detected on the classpath:

jackson-datatype-jdk7: support for
Java 7 types like 
java.nio.file.Path
.

jackson-datatype-joda: support for
Joda-Time types.

jackson-datatype-jsr310: support
for Java 8 Date & Time API types.

jackson-datatype-jdk8: support for
other Java 8 types like 
Optional
.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  annotation-driver