這個註解類型指示Spring用哪個類或方法來處理請求動做,該註解能夠用於類或方法;html
@RequestMapping註解支持的屬性java
這個屬性將URL映射到方法上,因爲value屬性是@RequestMapping註解的默認屬性,所以若是隻有惟一屬性,則能夠省略屬性名;即以下兩個標註含義相同:
@RequestMapping("value="/hello") @RequestMapping("/hello")
但若是超過一個屬性,就必須寫上value屬性名稱git
該屬性用來指示該方法僅僅處理哪些HTTP請求方式,若是沒有指定method屬性值,則請求處理方法能夠處理任意的HTTP請求方式。
該屬性指定處理請求的提交內容類型(Content-Type)
@RequestMapping(value="/hello",method=RequestMethod.POST, consumes="application/json")
表示方法僅處理request Content-Type 爲"application/json"類型的請求。
該屬性指定返回的內容類型,返回的內容類型必須是request請求頭(Accept)中所包含的類型。
@RequestMapping(value="/hello",method=RequestMethod.POST, produces="application/json")
方法僅處理request請求中Accept頭中包含了"application/json"的請求,同時指明瞭返回的內容類型爲application/json。
該屬性指定request中必須包含某些參數值時,才讓該方法處理。
@RequestMapping(value="/hello",method=RequestMethod.POST, params="myparam=myvalue")
方法僅處理其中名爲"myparam"、值爲"myvalue"的請求。
該屬性指定request中必須包含某些指定的header值,才能讓該方法處理請求。
@RequestMapping(value="/hello",method=RequestMethod.POST, headers="Referer=http://www.ccgogoing.github.io")
方法僅處理request的header中包含了指定'Referer'請求頭和對應值爲"http://www.ccgogoing.github.io"的請求。
Spring MVC在內部使用了一個org.springframework.ui.Model接口存儲模型數據,它的功能相似java.util.Map接口,可是比Map易於使用。org.springframework.ui.ModelMap接口實現了Map接口。github
Spring MVC在調用處理方法以前會建立一個隱含的模型對象,做爲模型數據的存儲容器。若是處理方法的參數爲Model或ModelMap類型,則Spring MVC會將隱含模型的引用傳遞給這些參數。在處理方法內部,開發者就能夠經過這個參數對象訪問模型中的全部數據,也能夠向模型中添加新的屬性數據。web
在處理方法中,Model和ModelMap對象均可以使用以下方法添加模型數據:addObject(String attributeName,Object attributeValue)
spring
org.springframework.web.bind.annotation.RequestParam 註解類型用於將指定的請求參數賦值給方法中的形參。
使用@RequestParam 註解可指定以下表所示的屬性:json
屬性 | 類型 | 是否必要 | 是否說明 |
---|---|---|---|
name | String | 否 | 指定請求頭綁定的名稱 |
value | String | 否 | name屬性的別名 |
required | boolean | 否 | 指定參數是否必須綁定 |
defaultValue | String | 否 | 若是沒有傳遞參數而使用的默認值 |
org.springframework.web.bind.annotation.PathVariale 註解類型能夠很是方便地得到請求URL中的動態參數。@PathVariable註解只支持一個屬性value,類型爲String,表示綁定的名稱,若是省略則默認綁定同名參數.示例代碼以下:
@RequestMapping(value="/pathVariableTest/{userId}") public void pathVariableTest(@PathVariable Integer userId)
URL模版變量{userId}綁定到經過@PathVariable註解的同名參數上mvc
org.springframework.web.bind.annotation.RequestHeader註解類型用於將請求的頭信息區數據映射到功能處理方法的參數上。
org.springframework.web.bind.annotation.CookieValue用於將請求的Cookie數據映射到功能處理方法的參數上。
org.spingframework.web.bind.annotation.SessionAttribute註解類型容許咱們有選擇地指定Model中的哪些屬性須要轉存到HttpSession對象當中。
屬性 | 類型 | 是否必要 | 是否說明 |
---|---|---|---|
names | String[] | 否 | Model中屬性的名稱,即存儲在HttpSession當中的屬性名稱 |
value | String[] | 否 | names屬性的別名 |
types | Class<?>[] | 否 | 指定放入對象的類型 |
@SessionAttributes 只能聲明在類上,而不能聲明在方法上。
使用方法以下:app
// 將Model中的屬性名爲user的屬性放入HttpSession對象當中 @SessionAttributes("user") @SessionAttributes(types={User.class},value="user") // 也能夠設置多個對象到HttpSession當中: @SessionAttributes(types={User.class,Dept.class},value={"user","dept"}) types屬性用來指定放入HttpSession當中的對象類型。
HttpMessageConverter< T >是Spring3.0以後新增的一個重要接口,它負責將請求信息轉換爲一個對象(類型爲T),並將對象(類型爲T)綁定到請求方法的參數中或輸出爲響應信息。
DispatcherServlet默認已經裝配了RequestMappingHandlerAdapter做爲HandlerAdapter組件的實現類,即HttpMessageConverter
由RequestMappingHandlerAdapter
使用,將請求信息轉換爲對象,或將對象轉換爲響應信息。
HttpMessageConverter< T >接口中定義瞭如下幾個方法:dom
- boolean canRead(Class< ? > clazz,MediaType mediaType)。該方法指定轉換器能夠讀取的對象類型,即轉換器可將請求信息轉換爲clazz類型的對象,同時指定支持的MIME類型(text/html、application/json等)。MIME媒體類型在RFC2616中定義,具體請參考https://tools.ietf.org/html/rfc2616#section-3.7
- boolean canWrite(Class< ? >clazz, MediaType mediaType)。該方法指定轉換器能夠將clazz類型的對象寫到響應流當中,響應流支持的媒體類型在mediaType中定義。
- List<MediaType> getSupportedMediaTypes()。該方法返回當前轉換器支持的媒體類型。
- T read(Class< ? extends T> clazz,HttpInputMessage inputMessage)。該方法將請求信息流轉換爲T類型的對象。
- void write (T t,MediaType contentType,HttpOutputMessage outMessage)。該方法將T類型的對象寫到響應流當中,同時指定響應的媒體類型爲contentType。
Spring爲HttpMessageConverter< T> 提供了多個實現類,這些實現類組成了一個功能強大、用途普遍的信息轉換家族:
StringHttpMessageConverter
。 將請求信息轉換爲字符串。泛型T爲String類型,能夠讀取全部媒體類型( /)的請求信息,可經過設置supportedMediaTypes屬性指定媒體類型。響應信息的媒體類型爲text/plain(即Content-Type的值)。FormHttpMessageConverter
。 將表單數據讀取到MultiValueMap中。泛型T爲org.springframework.util.MultiValueMap< String,? >類型,支持讀取application/x-www-form-urlencoded的類型,但不支持讀取multipart/form-data的類型。能夠寫application/x-www-form-urlencoded及multipart/form-data類型的響應信息。XmlAwareFormHttpMessageConverter
。繼承自FormHttpMessageConverter,若是部分表單屬性是XML數據,則能夠用該轉換器進行轉換。ResourceHttpMessageConverter
。讀寫org.springframework.core.io.Resource
對象。泛型T爲org.springframework.core.io.Resource對象,能夠讀取全部媒體類型( /)的請求信息。若是類路徑下提供了JAF(Java Activation Framework),則根據Resource的類型指定響應的類型,不然響應的類型爲application/octet-stream
。BufferedImageHttpMessageConverter
。讀寫BufferedImage對象。泛型T爲BufferedImage對象,能夠讀取全部類型( /)的請求信息,返回BufferedImage相應的類型,也能夠經過contentType顯示指定。ByteArrayHttpMessageConverter
。讀寫二進制數據。泛型T爲byte[]類型,能夠讀取全部類型( /)的請求信息,能夠設置supportMediaTypes屬性指定類型,響應信息的媒體類型爲application/octet-stream。SourceHttpMessageConverter
。讀寫javax.xml.transform.Source類型的數據。泛型T爲javax.xml.transform.Source
類型及其擴展類,包括javax.xml.transform.dom.DOMSource
、javax.xml.transform.sax.SAXSource
、javax.xml.transform.stream.StreamSource
,能夠讀取text/xml和application/xml類型請求,響應信息的類型爲text/xml和application/xml。MarshallingHttpMessageConverter
。經過Spring 的 org.springframework.oxm.Marshalling(將Java對象轉換成XML)和 Unmarshaller(將XML解析爲Java對象)讀寫XML消息。泛型T爲Object類型,能夠讀取text/xml和application/xml類型請求,響應消息的類型爲text/xml和application/xml。Jaxb2RootElementHttpMessageConverter
。經過JAXB2讀寫XML消息,將請求消息轉換到註解XmlRootElement和XmlType做用的類中。泛型T爲Object類型,能夠讀取text/xml和application/xml類型的請求,響應消息的類型爲text/xml和application/xml。MappingJackson2HttpMessageConverter
。利用Jackson開源類包讀寫JSON數據。泛型T爲Object類型,能夠讀取application/json類型數據,響應信息的類型爲application/jspon。RequestMappingHandlerAdapter默認已經裝配了如下的HttpMessageConverter:
- StringHttpMessageConverter
- ByteArrayHttpMessageConverter
- SourceHttpMessageConverter
- XmlAwareFormHttpMessageConverter
若是須要裝配其餘類型的HttpMessageConverter,則能夠再Spring的Web容器的上下文中自行定義一個RequestMappingHandlerAdapter,以下所示:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> </list> </property> </bean>
注意:若是Spring Web容器中顯示定義了一個RequestMappingHandlerAdapter,則Spring MVC 的RequestMappingHandlerAdapter默認裝配的HttpMessageConverter將再也不起做用。
Spring MVC提供了處理JSON格式請求/響應的HttpMessageConverter:
MappingJackson2HttpMessageConverter。利用Jackson開源類包處理JSON格式的請求或響應消息。
所以只須要在Spring Web容器中爲RequestMappingHandlerAdapter裝配處理JSON的HttpMessageConverter,並在交互過程當中經過請求的Accept指定MIME類型,Spring MVC就可使服務端的處理方法和客戶端JSON格式的消息進行通訊了。
org.springframework.web.bind.annotation.RequestBody
註解用於讀取Request請求的body部分數據,使用系統默認配置的HttpMessageConverter進行解析,而後把相應的數據綁定到Controller中方法的參數上。
當前臺頁面使用GET 或 POST 方式提交數據時,數據編碼格式由請求頭的ContentType指定。能夠分爲以下幾種狀況: