@requestmapping的參數(shù)和用法
requestmapping里面的注解包含的參數(shù)如圖:
requestmapping是一個(gè)用來(lái)處理請(qǐng)求地址映射的注解,可用于類(lèi)或方法上。用于類(lèi)上,表示類(lèi)中的所有響應(yīng)請(qǐng)求的方法都是以該地址作為父路徑。
@requestmapping 除了修飾方法, 還可來(lái)修飾類(lèi) :
類(lèi)定義處:提供初步的請(qǐng)求映射信息。相對(duì)于 web 應(yīng)用的根目錄;
方法處:提供進(jìn)一步的細(xì)分映射信息。 相對(duì)于類(lèi)定義處的 url。
若類(lèi)定義處未標(biāo)注 @requestmapping,則方法處標(biāo)記的 url相對(duì)于 web 應(yīng)用的根目錄
返回modelandview時(shí)的url會(huì)根據(jù)你的 @requestmapping實(shí)際情況組成。
如果類(lèi)上沒(méi)有映射,那么url直接就是方法的映射;否則url為類(lèi)上+方法上映射路徑組合。
對(duì)應(yīng)項(xiàng)目jsp位置則是一級(jí)路徑對(duì)應(yīng)一級(jí)文件目錄。
如url為/default/index對(duì)應(yīng)項(xiàng)目中webapp/default/index.jsp
requestmapping注解有六個(gè)屬性,下面我們把她分成三類(lèi)進(jìn)行說(shuō)明
【1】value, method
value
:指定請(qǐng)求的實(shí)際地址,指定的地址可以是uri template 模式;
method
: 指定請(qǐng)求的method類(lèi)型, get、post、put、delete等;
【2】consumes,produces
consumes
: 指定處理請(qǐng)求的提交內(nèi)容類(lèi)型(content-type),例如application/json, text/html;
produces
:指定返回的內(nèi)容類(lèi)型,僅當(dāng)request請(qǐng)求頭中的(accept)類(lèi)型中包含該指定類(lèi)型才返回;
【3】params,headers
params
: 指定request中必須包含某些參數(shù)值時(shí),才讓該方法處理。
headers
: 指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求。
測(cè)試示例如下:
【1】value||path
- jsp 頁(yè)面
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錯(cuò)誤。
value的uri值為以下三類(lèi):
a) 可以指定為普通的具體值;
如下:
1
|
@requestmapping ( "/testrequestmapping" ) |
b) 可以指定為含有某變量的一類(lèi)值(uri template patterns with path variables)–restful風(fēng)格;
1
2
3
4
5
|
@requestmapping ( "/testpathvariable/{id}" ) public string testpathvariable( @pathvariable integer id2) { system.out.println( "testpathvariable: " + id2); return success; } |
除了value還有path,二者效果等同,可以參考源碼如下圖:
其中關(guān)于@pathvariable 有如下說(shuō)明:
① 如果路徑中的變量與方法中的變量名一致,可直接使用@pathvariable;
② 如果二者不一致,則使用@pathvariable(variable)顯示指定要綁定的路徑中的變量 。
@pathvariable只能綁定路徑中的占位符參數(shù),且路徑中必須有參數(shù)。
@pathvariable用法參考 路徑參數(shù)綁定參考
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) 可以指定為含正則表達(dá)式的一類(lèi)值( 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 頁(yè)面
1
2
|
<a href= "springmvc/testmethod" rel= "external nofollow" >test method</a> //href 默認(rèn)為get 請(qǐng)求。 |
- controller–限制接收post 請(qǐng)求。
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 。
【狀態(tài)碼405表示:請(qǐng)求中指定的方法不被允許。】
將method 改為method = requestmethod.get正常跳轉(zhuǎn)頁(yè)面。
【3】consumes
- jsp 頁(yè)面
仍以testmethod為例,提交表單。
默認(rèn)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 請(qǐng)求以及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 :
【狀態(tài)碼415表示:由于媒介類(lèi)型不被支持,服務(wù)器不會(huì)接受請(qǐng)求。。】
去掉 consumes屬性,頁(yè)面正常跳轉(zhuǎn) !
【4】produces
后臺(tái)代碼如下:
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); } |
- 瀏覽器請(qǐng)求頭
1
|
text/html,application/xhtml+xml,application/xml;q= 0.9 ,image/webp,*/*;q= 0.8 |
其中最后一項(xiàng) : */*;q=0.8。
該項(xiàng)表明可以接收任何類(lèi)型的,權(quán)重系數(shù)0.8表明如果前面幾種類(lèi)型不能正常接收。則使用該項(xiàng)進(jìn)行自動(dòng)分析。
application/json 幾種主流瀏覽器都可以自動(dòng)解析。
【5】params
- jsp頁(yè)面
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> |
參數(shù) username=tom;age = 10;
- 后臺(tái)代碼:
設(shè)定必須包含username 和age兩個(gè)參數(shù),且age參數(shù)不為10 (可以有多個(gè)參數(shù))。
1
2
3
4
5
|
@requestmapping (value = "testparamsandheaders" , params = { "username" , "age!=10" }) public string testparamsandheaders() { system.out.println( "testparamsandheaders" ); return success; } |
- result as follows :
【狀態(tài)碼400表示:服務(wù)器未能理解請(qǐng)求。 】
- 將age 改為其他值,正常跳轉(zhuǎn)。
【6】headers
- 瀏覽器請(qǐng)求頭如下:
- 后臺(tái)測(cè)試代碼如下:
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; } |
設(shè)定請(qǐng)求頭中第一語(yǔ)言必須為us。
- result as follows :
【狀態(tài)碼404表示:服務(wù)器無(wú)法找到被請(qǐng)求的頁(yè)面。】
將后臺(tái)代碼改為zh-cn。。。
頁(yè)面正常跳轉(zhuǎn)。
【tips】:
① 服務(wù)器首先根據(jù)url去找頁(yè)面,如果找不到就返回404;
② 如果找到,但是不能正常處理,就會(huì)返回 5xx 類(lèi)型錯(cuò)誤。
其中在第一步過(guò)程中,會(huì)根據(jù)請(qǐng)求頭進(jìn)行一系列判斷 !
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://janus.blog.csdn.net/article/details/55193269