@requestmapping的參數和用法
requestmapping里面的注解包含的參數如圖:
requestmapping是一個用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。
@requestmapping 除了修飾方法, 還可來修飾類 :
類定義處:提供初步的請求映射信息。相對于 web 應用的根目錄;
方法處:提供進一步的細分映射信息。 相對于類定義處的 url。
若類定義處未標注 @requestmapping,則方法處標記的 url相對于 web 應用的根目錄
返回modelandview時的url會根據你的 @requestmapping實際情況組成。
如果類上沒有映射,那么url直接就是方法的映射;否則url為類上+方法上映射路徑組合。
對應項目jsp位置則是一級路徑對應一級文件目錄。
如url為/default/index對應項目中webapp/default/index.jsp
requestmapping注解有六個屬性,下面我們把她分成三類進行說明
【1】value, method
value
:指定請求的實際地址,指定的地址可以是uri template 模式;
method
: 指定請求的method類型, get、post、put、delete等;
【2】consumes,produces
consumes
: 指定處理請求的提交內容類型(content-type),例如application/json, text/html;
produces
:指定返回的內容類型,僅當request請求頭中的(accept)類型中包含該指定類型才返回;
【3】params,headers
params
: 指定request中必須包含某些參數值時,才讓該方法處理。
headers
: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。
測試示例如下:
【1】value||path
- jsp 頁面
1
|
<a href= "springmvc/testrequestmapping" rel= "external nofollow" >test requestmapping</a> |
- controller
1
2
3
4
5
|
@requestmapping (value= "/testrequestmapping" ) public string testrequestmapping() { system.out.println( "testrequestmapping" ); return success; } |
成功返回success.jsp 。
tips :若 href 屬性值,不等于value值,則將提示404錯誤。
value的uri值為以下三類:
a) 可以指定為普通的具體值;
如下:
1
|
@requestmapping ( "/testrequestmapping" ) |
b) 可以指定為含有某變量的一類值(uri template patterns with path variables)–restful風格;
1
2
3
4
5
|
@requestmapping ( "/testpathvariable/{id}" ) public string testpathvariable( @pathvariable integer id2) { system.out.println( "testpathvariable: " + id2); return success; } |
除了value還有path,二者效果等同,可以參考源碼如下圖:
其中關于@pathvariable 有如下說明:
① 如果路徑中的變量與方法中的變量名一致,可直接使用@pathvariable;
② 如果二者不一致,則使用@pathvariable(variable)顯示指定要綁定的路徑中的變量 。
@pathvariable只能綁定路徑中的占位符參數,且路徑中必須有參數。
@pathvariable用法參考 路徑參數綁定參考
1
2
3
4
5
6
|
@requestmapping ( "/testpathvariable/{id}" ) public string testpathvariable( @pathvariable ( "id" ) integer id2) { system.out.println( "testpathvariable: " + id2); return success; } //路徑中的 id 與 方法中的 id2 綁定 |
c) 可以指定為含正則表達式的一類值( uri template patterns with regular expressions);
1
2
3
4
5
|
@requestmapping ( "/spring-web/{symbolicname:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}" ) public void handle( @pathvariable string version, @pathvariable string extension) { // ... } } |
【2】method
- jsp 頁面
1
2
|
<a href= "springmvc/testmethod" rel= "external nofollow" >test method</a> //href 默認為get 請求。 |
- controller–限制接收post 請求。
1
2
3
4
5
|
@requestmapping (value = "/testmethod" , method = requestmethod.post) public string testmethod() { system.out.println( "testmethod" ); return success; } |
- result as follows :
http status 405 - request method ‘get' not supported 。
【狀態碼405表示:請求中指定的方法不被允許。】
將method 改為method = requestmethod.get正常跳轉頁面。
【3】consumes
- jsp 頁面
仍以testmethod為例,提交表單。
默認contenttype為content-type:application/x-www-form-urlencoded。
1
2
3
4
|
<form action= "springmvc/testmethod" method= "post" > <input type= "text" name= "username" value= "" /> <input type= "submit" value= "submit" /> </form> |
- controller–限制接收post 請求以及consumes="application/json"。
1
2
3
4
5
|
@requestmapping (value = "/testmethod" , method = requestmethod.post,consumes= "application/json" ) public string testmethod() { system.out.println( "testmethod" ); return success; } |
- result as follows :
【狀態碼415表示:由于媒介類型不被支持,服務器不會接受請求。。】
去掉 consumes屬性,頁面正常跳轉 !
【4】produces
后臺代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@requestmapping (value = "/testmethod" , method = requestmethod.post,produces= "application/json" ) public void testmethod2(httpservletrequest request,httpservletresponse response,model model) throws ioexception { request.getheader( "accept" ); system.out.println(request.getheader( "accept" )); // response.setcontenttype("application/json"); string username = request.getparameter( "username" ); system.out.println( "testmethod..." +username); model.addattribute( "user" , username); object jsonstring = "{'name': 'helloworlda'}" ; jsonobject jsonobj=jsonobject.fromobject(jsonstring); printwriter out = response.getwriter(); out.print(jsonobj); } |
- 瀏覽器請求頭
1
|
text/html,application/xhtml+xml,application/xml;q= 0.9 ,image/webp,*/*;q= 0.8 |
其中最后一項 : */*;q=0.8。
該項表明可以接收任何類型的,權重系數0.8表明如果前面幾種類型不能正常接收。則使用該項進行自動分析。
application/json 幾種主流瀏覽器都可以自動解析。
【5】params
- jsp頁面
1
2
3
4
5
|
form action= "springmvc/testparamsandheaders" method= "post" > <input type= "text" name= "username" value= "" /> <input type= "text" name= "age" value= "" /> <input type= "submit" value= "submit" /> </form> |
參數 username=tom;age = 10;
- 后臺代碼:
設定必須包含username 和age兩個參數,且age參數不為10 (可以有多個參數)。
1
2
3
4
5
|
@requestmapping (value = "testparamsandheaders" , params = { "username" , "age!=10" }) public string testparamsandheaders() { system.out.println( "testparamsandheaders" ); return success; } |
- result as follows :
【狀態碼400表示:服務器未能理解請求。 】
- 將age 改為其他值,正常跳轉。
【6】headers
- 瀏覽器請求頭如下:
- 后臺測試代碼如下:
1
2
3
4
5
|
@requestmapping (value = "testparamsandheaders" , params = { "username" , "age!=10" }, headers = { "accept-language=us,zh;q=0.8" }) public string testparamsandheaders() { system.out.println( "testparamsandheaders" ); return success; } |
設定請求頭中第一語言必須為us。
- result as follows :
【狀態碼404表示:服務器無法找到被請求的頁面。】
將后臺代碼改為zh-cn。。。
頁面正常跳轉。
【tips】:
① 服務器首先根據url去找頁面,如果找不到就返回404;
② 如果找到,但是不能正常處理,就會返回 5xx 類型錯誤。
其中在第一步過程中,會根據請求頭進行一系列判斷 !
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://janus.blog.csdn.net/article/details/55193269