requestmapping注解說明
@requestmapping注解的作用將web請求映射到特定處理程序類和/或處理程序方法,這個注解可以用于類或者方法上,并通過屬性value指定請求路徑。用在controller類上表示提供初步的url請求映射信息,相對于web應用的根目錄,這是一個前置請求路徑。用在controller中方法上,表示提供詳細的url映射。如果controller類上沒有加requestmapping注解,則方法上注解標記的url則是相對于web應用的根目錄。
@requestmapping注解提供以下幾個屬性:
name:用于指定映射器名稱
value:用于指定映射路徑,同path
path:用于指定映射路徑,同value
method:用于指定請求類型:get, post, head, options, put, patch, delete, trace
params:指定請求的參數
headers:指定請求頭部,源碼示例:requestmapping(value = "/something", headers = "content-type=text/*")
consumes:指定處理請求提交的內容類型(content-type),例如application/json, text/html,只有在content-type匹配這些媒體類型之一時才會映射請求
produces:指定請求返回的內容類型 例如:produces = "application/json; charset=utf-8"
通過value屬性指定映射路徑
controller類上使用requestmapping注解
1
2
3
4
5
6
7
8
9
|
@controller @requestmapping ( "order" ) public class orderinfocontroller { //示例1 @requestmapping ( "orderinfo" ) public modelandview orderinfo1() { return new modelandview( "order/info" , "message" , "orderinfo" ); } } |
在ordercontroller類上添加了注解requestmapping("order"),表示所有對的請求必須是以“根目錄/order” 開始
示例1的請求路徑為:http://localhost:8080/springmvcnext/order/orderinfo
示例1 如果注釋掉controller上的@requestmapping("order"),則對應的請求路徑為:http://localhost:8080/springmvcnext /orderinfo
controller方法上使用requestmapping注解
1.常用基礎用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@controller @requestmapping ( "order" ) public class orderinfocontroller { //示例1 @requestmapping ( "orderinfo" ) public modelandview orderinfo1() { return new modelandview( "order/info" , "message" , "orderinfo" ); } //示例2 :處理多個url映射 @requestmapping ({ "info" , "index" }) //或者@requestmapping(value={"info","index"}) public modelandview orderinfo2() { return new modelandview( "order/info" , "message" , "orderinfo2" ); } //示例3 @requestmapping public modelandview orderinfo3() { return new modelandview( "order/info" , "message" , "orderinfo3" ); } } |
requestmapping只配置value屬性,不顯示配置其他屬性的情況下,value省略,直接填寫url映射信息即可,指定其他屬性的情況下value屬性必須明確填寫
上例示例1的訪問路徑為: http://localhost:8080/springmvcnext/order/orderinfo
示例2:requestmapping接口中value屬性是一個數組,所有也支持傳一個數組 示例2的訪問路徑:http://localhost:8080/springmvcnext/order/index 或者 http://localhost:8080/springmvcnext/order/info
示例3:當value為空時,表示該方法為類下默認的action,示例3的訪問路徑為:http://localhost:8080/springmvcnext/order
2.url模板映射
在requestmapping注解中聲明uri變量,并通過@pathvariable注解的方式訪從實際請求url中獲取值,示例如下:
1
2
3
4
5
6
7
8
|
@controller public class orderinfocontroller { // 示例10 帶占位符的url @requestmapping (value = "user/{userid}/order/{ordernumber}" , method = requestmethod.get) public modelandview orderinfo4( @pathvariable int userid, @pathvariable string ordernumber) { return new modelandview( "order/info" , "message" , "userid:" +userid+ " ordernumber:" +ordernumber); } } |
示例10請求url: http://localhost:8080/springmvcnext/user/12/order/333 當通過此url發起請求時,springmvc將通過@pathvariable可以提取url模板中的{×××}中的×××變量, url變量會自動轉換為對應的類型,無法轉換的則返回錯誤,比如嘗試用以下url訪問:http://localhost:8080/springmvcnext/user/xxx/order/333 其中參數userid=xxx,則發生錯誤:
3.ant風格的url路徑映射
ant風格通配符如下:
- ? 匹配一個字符
- * 匹配路徑段中的零個或多個字符
- ** 匹配零個或多個路徑段
示例:
1
2
3
4
5
6
7
8
9
10
|
@controller public class orderinfocontroller { // 示例11 帶占位符的url @requestmapping (value = "order*" , method = requestmethod.get) //@requestmapping(value = "order?", method = requestmethod.get) //@requestmapping(value = "order/**", method = requestmethod.get) public modelandview orderinfo5(string ordernumber) { return new modelandview( "order/info" , "message" , "orderinfo5" ); } } |
示例11請求url: http://localhost:8080/springmvcnext/order/orderdexx?ordernumber=12 可以匹配http://localhost:8080/springmvcnext/order/orderxxxxx?ordernumber=yyyy的所有請求
1
2
3
|
@requestmapping (value = "order?" , method = requestmethod.get)可以匹配諸如 “…/ordera?ordernumber….” “…/orders?ordernumber….” @requestmapping (value = "order/**" , method = requestmethod.get)可以匹配諸如 “…/order/aaa?ordernumber….” “…/order/bbb/ccc?ordernumber….” |
另外 requestmapping還支持正則表達式風格的url路徑映射,此處略過
通過method屬性指定請求類型
requestmapping提供的method屬性請求謂詞的類型,如下示例示例只接受get請求
1
2
3
4
5
|
// 示例4 @requestmapping (value= "detail" ,method=requestmethod.get) //也可直接使用 @getmapping("detail") public modelandview info() { return new modelandview( "order/info" , "message" , "info" ); } |
對于每種請求類型,springmvc還提供了專用的注解:
@getmapping
@postmapping
@putmapping
@deletemapping
@patchmapping
通過params指定參數名或參數值約束
params屬性可以限定請求參數包含特定的參數,也可限定參數值的約束,如下代碼所示:
1
2
3
4
5
6
7
8
9
10
|
// 示例5 params 限定參數包含ordernumber @requestmapping (value = "detail2" , params = "ordernumber" ) public modelandview detail2(string ordernumber) { return new modelandview( "order/info" , "message" , ordernumber); } // 示例6 params 限定參數值 @requestmapping (value = "detail3" , params = "ordernumber!=1222" ) public modelandview detail3(string ordernumber) { return new modelandview( "order/info" , "message" , ordernumber); } |
示例5限定請求參數必須包含參數ordernumber,如果不包含名為ordernumber的參數,則拒絕訪問:訪問路徑:http://localhost:8080/springmvcnext/order/detail2?ordernumber=12
示例6限定請求參數必須包含參數ordernumber并且參數值不能為1222 訪問路徑:http://localhost:8080/springmvcnext/order/detail3?ordernumber=1222 時報錯
通過headers指定參數名或參數值約束
requestmapping提供的method屬性可以指定請求頭類型,只有請求數據頭部類型符合指定的值時,才能正常訪問
1
2
3
4
5
6
|
// 示例7 params 限定參數值 @requestmapping (value = "headtest" ,headers = "apikey=23131313" ) //@requestmapping(value = "headtest",headers= {"accept=application/json"}) public modelandview header() { return new modelandview( "order/info" , "message" , "header" ); } |
示例7限定請求頭必須包含apikey:23131313才可以正常返回,直接訪問,返回錯誤:
添加添加header信息apikey:23131313訪問成功:
通過consumes指定請求提交的內容類型(content-type)
1
2
3
4
5
|
// 示例8 consumes @requestmapping (value = "consumes" , method = requestmethod.post, consumes = "application/json" ) public modelandview consumes(string ordernumber) { return new modelandview( "order/info" , "message" , ordernumber); } |
示例限定請求參數類型為application/json,表示該方法只處理請求content-type為application/json的請求:
下面通過拋postman測試:
設置請求參數格式為application/json,可以正常訪問:
設置參數格式為x-form-urlencoded,返回錯誤,http status 415
通過produces指定返回的內容類型(content-type)
produces屬性用于設定返回內容類型,并且滿足以下條件:接受請求header中包含accept的值與produces設定的值相同,或者接受的請求使用不顯示設置accept值
1
2
3
4
5
|
// 示例8 produces 限定返回數據application/json @requestmapping (value = "produces" , method = requestmethod.get, produces = "application/json" ) public modelandview produces(string ordernumber) { return new modelandview( "order/info" , "message" , ordernumber); } |
示例8 表示返回內容格式application/json ,當客戶端設置的accept格式為text/json時,運行報錯,http status 406
當客戶端設置的accept格式為application/json或者不設置accept值時,可以正常運行
總結
以上所述是小編給大家介紹的spring mvc溫故而知新系列教程之請求映射requestmapping注解,希望對大家有所幫助http://localhost:8080/springmvcnext/order/orderinfo
示例2:requestmapping接口中value屬性是一個數組,所有也支持傳一個數組 示例2的訪問路徑:http://localhost:8080/springmvcnext/order/index 或者 http://localhost:8080/springmvcnext/order/info
示例3:當value為空時,表示該方法為類下默認的action,示例3的訪問路徑為:http://localhost:8080/springmvcnext/order