SpringMVC?@RequestMapping注解詳解
一、@RequestMapping
@RequestMapping注解的源碼:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
1.@RequestMapping注解的功能
@RequestMapping的功能就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。
SpringMVC接收到指定的請求,就會來找到在映射關(guān)系中對應(yīng)的控制器方法來處理這個請求。
2.@RequestMapping注解的位置
@RequestMapping標注在類:設(shè)置映射請求請求路徑的初始信息(通常用于表示某個模塊的信息)
@RequestMapping標注在方法:設(shè)置映射請求請求路徑的具體信息
@RequestMapping相同的請求地址一定只有一個RequestMapping映射,否則會報錯。
瀏覽器先匹配類上標注的@Requestmapping,再匹配方法上的@Requestmapping,即請求地址url = 類請求路徑 + 方法請求路徑
@Controller
@RequestMapping("/hello")
public class DisplayController {
@RequestMapping("/displayTest")
//此時的請求實際就是/hello/displayTest
public String displayTest(){
return "display";
}
}
此時需要映射的請求地址實際就是http://localhost:8080/hello/display
<!--瀏覽器解析的是絕對路徑,會將"/"解析為"localhost:8080/"-->
<a th:href="@{/hello/displayTest}" rel="external nofollow" >訪問display頁面...</a>
二、@RequestMapping注解的屬性
1.value屬性(掌握)
value屬性是通過請求地址來匹配請求映射(默認是value),value屬性是一個String類型的數(shù)組,表示該請求能夠匹配多個請求地址所對應(yīng)的請求,value屬性必須設(shè)設(shè)置,至少通過請求地址映射來匹配請求映射。
//表示可以通過地址testWorld或者hello/testWorld都可以訪問world.html
//@RequestMapping請求地址與方法名沒有關(guān)系,但是為了方便習(xí)慣上使用方法名
@RequestMapping(value = {"testWorld","hello/testWorld"})
public String testWorld(){
return "world";
}
此時需要映射的請求地址實際就是http://localhost:8080/testWorld或者http://localhost:8080/hello/testWorld
<!--瀏覽器解析的是絕對路徑,會將"/"解析為"localhost:8080/"-->
<a th:href="@{/hello/testWorld}" rel="external nofollow" >@{/hello/testWorld}訪問helloworld頁面...</a><br>
<a th:href="@{/testWorld}" rel="external nofollow" rel="external nofollow" >@{/testWorld}訪問helloworld頁面...</a><br>
2.method屬性(掌握)
method是通過請求方式(get/post)匹配請求映射,method屬性是一個RequestMethod[]類型的數(shù)組,表示該種請求可以匹配多種請求方式對應(yīng)的請求。如果當前請求的請求映射滿足value屬性,但是不滿足method屬性,就會報錯405,報錯:Request method 'POST' not supported
get和post兩種常見的請求方式有何區(qū)別?
get:使用?將請求參數(shù)與請求地址拼接起來,格式:?請求參數(shù)名=請求參數(shù)值&請求參數(shù)名=請求參數(shù)值,不安全,傳輸速度快(請求地址也會傳輸),傳輸數(shù)據(jù)量有限,不能用于上傳文件
post:請求參數(shù)放在請求體中(格式:請求參數(shù)名=請求參數(shù)值&請求參數(shù)名=請求參數(shù)值),安全,傳輸速度慢,傳輸數(shù)據(jù)量大
//表示可以通過地址testWorld或者hello/testWorld都可以訪問world.html
@RequestMapping(value = {"testWorld","hello/testWorld"},method = {RequestMethod.GET,RequestMethod.POST})
public String testWorld(){
return "world";
}
<!--瀏覽器解析的是絕對路徑,會將"/"解析為"localhost:8080/"-->
<a th:href="@{/testWorld}" rel="external nofollow" rel="external nofollow" >@{/testWorld}訪問helloworld頁面(GET請求(默認))</a><br>
<form th:action="@{/testWorld}" method="post">
<input type="submit" value="@{/testWorld}訪問helloworld頁面(POST請求)">
</form>
此時訪問get請求正常,訪問post請求,請求方式不匹配(請求不支持),報錯誤405

將WorldController的@RequestMapping改為@RequestMapping(value = {"testWorld","hello/testWorld"},method = {RequestMethod.GET,RequestMethod.POST}),再次訪問post和get請求,請求成功。

3.params屬性(了解)
params屬性是指通過請求的參數(shù)匹配處理請求的映射,params屬性是一個String類型的數(shù)組,表示通過四種表達式設(shè)置請求參數(shù)和請求映射匹配關(guān)系。
“param”:要求請求映射所匹配的請求必須攜帶param請求參數(shù)
“!param”:要求請求映射所匹配的請求必須不能攜帶param請求參數(shù)
“param=value”:要求請求映射所匹配的請求必須攜帶param請求參數(shù)且param=value
“param!=value”:要求請求映射所匹配的請求必須攜帶param請求參數(shù)但是param!=value
//處理請求的參數(shù)只能是username和password,且username=admin、password=12345678
@RequestMapping(value = "/testParams",params = {"username=admin","password=12345678"})
public String testParams(){
return "world";
}
<!--瀏覽器解析的是絕對路徑,會將"/"解析為"localhost:8080/"-->
<!--此處使用單引號,雙引號會報錯-->
<!--會自動轉(zhuǎn)化為?傳參-->
<a th:href="@{/testParams(username='admin',password=12345678)}" rel="external nofollow" rel="external nofollow" >測試params參數(shù)</a><br>
可以看到請求參數(shù)已經(jīng)使用?進行了拼接,請求訪問成功,

將請求改為如下,再次訪問,@RequestMapping請求參數(shù)不匹配,報400
<a th:href="@{/testParams(username='root',password=12345678)}" rel="external nofollow" >測試params參數(shù)root</a><br>

4.headers屬性(了解)
headers屬性通過請求的請求頭信息匹配請求映射,headers屬性是一個String類型的數(shù)組,表示可以通過四種表達式設(shè)置請求頭信息和請求映射的匹配信息。
“header”:要求請求映射所匹配的請求必須攜帶header請求頭信息
“!header”:要求請求映射所匹配的請求必須不能攜帶header請求頭信息
“header=value”:要求請求映射所匹配的請求必須攜帶header請求頭信息且header=value
“header!=value”:要求請求映射所匹配的請求必須攜帶header請求頭信息且header!=value
若當前請求滿足@RequestMapping注解的value和method屬性,但是不滿足headers屬性,此時頁面顯示404錯誤,即資源未找到
//此時的請求的params中username=admin,password=1234545678,并且端口號是8081才可以成功,否則報錯
@RequestMapping(value = "/testParams",
params = {"username=admin","password=12345678"},
headers = {"host=localhost:8081"})
public String testParams(){
return "world";
}
<a th:href="@{/testParams(username='admin',password=12345678)}" rel="external nofollow" rel="external nofollow" >測試params參數(shù)</a><br>
可以看到未找到,@RequestMapping的請求頭參數(shù)不匹配,報錯誤404

將端口號改為8080,再次測試。
//此時的請求的params中username=admin,password=1234545678,并且端口號是8080才可以成功,否則報錯
@RequestMapping(value = "/testParams",
params = {"username=admin","password=12345678"},
headers = {"host=localhost:8080"})
public String testParams(){
return "world";
}
可以看到訪問成功

總結(jié):
@RequestMapping沒有和任意參數(shù)匹配,報錯誤404
@RequestMapping的請求頭參數(shù)不匹配,報錯誤404
@RequestMapping請求參數(shù)(params)不匹配,報錯誤400
@RequestMapping請求方式不匹配(請求不支持),報錯誤405
5.SpringMVC支持ant風格的路徑
?:表示任意的單個字符
*:表示任意的0個或多個字符
**:表示任意的一層或多層目錄
注意:在使用**時,只能使用/**/xxx的方式
//@RequestMapping(value = "/a?a/testAnt")
//@RequestMapping(value = "/a*a/testAnt")
@RequestMapping(value = "/**/testAnt")
public String testAnt(){
return "world";
}
<a th:href="@{/a1a/testAnt}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >測試ant風格--->?</a><br>
<a th:href="@{/a1a/testAnt}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >測試ant風格--->*</a><br>
<a th:href="@{/a1a/testAnt}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >測試ant風格--->**</a><br>
6.SpringMVC支持路徑中的占位符(重點)
原始方式:/deleteUser?id=1
rest方式::/deleteUser/1
SpringMVC路徑中的占位符常用于restful風格中,當請求路徑中將某些數(shù)據(jù)通過路徑的方式傳輸?shù)椒?wù)器中,就可以在相應(yīng)的@RequestMapping注解的value屬性中通過占位符{xxx}表示傳輸?shù)臄?shù)據(jù),在通過@PathVariable注解,將占位符所表示的數(shù)據(jù)賦值給控制器方法的形參。
@RequestMapping("/testRest/{username}/{password}")
public String testRest(@PathVariable("username") String username,@PathVariable("password") String password){
System.out.println("username="+username);
System.out.println("password="+password);
return "world";
}
<!--瀏覽器解析的是絕對路徑,會將"/"解析為"localhost:8080/"-->
<a th:href="@{/testRest/小王/123456}" rel="external nofollow" >測試rest占位符</a><br>
可以看到?jīng)]有使用?連接,使用了rest方式實現(xiàn)了訪問。

查看idea的控制臺,數(shù)據(jù)通過路徑的方式傳輸?shù)椒?wù)器中。

三、@RequestMapping的派生類注解
對于處理指定請求方式的控制器方式,SpringMVC提供了@RequestMapping的派生類注解:
處理post請求的映射—>@PostMapping
處理get請求的映射—>@GetMapping
處理put請求的映射—>@PutMapping
處理delete請求的映射—>@DeleteMapping
但是目前瀏覽器只支持get和post,若在form表單提交時,為method設(shè)置了其他請求方式的字符串(put或delete),則按照默認的請求方式(get)處理。
@GetMapping("testGetMapping")
public String testGetMapping(){
return "world";
}
<!--瀏覽器解析的是絕對路徑,會將"/"解析為"localhost:8080/"-->
<a th:href="@{/testGetMapping}" rel="external nofollow" >@{/testGetMapping}訪問helloworld頁面...</a><br>

測試form表單是否支持put或delete方式的請求
當請求和請求映射的方式不匹配時就會報錯405.
//當前請求映射的請求地址是testPut,請求方式是put方式
@RequestMapping(value = "/testPut",method = RequestMethod.PUT)
public String testPut(){
return "world";
}
<!--測試form表單是否支持put或delete方式的請求-->
<form th:action="@{/testPut}" method="put">
<input type="submit" value="測試form表單是否支持put或delete方式的請求">
</form>
結(jié)果如下:
若在form表單提交時,為method設(shè)置了其他請求方式的字符串(put或delete),則按照默認的請求方式(get)處理。若要發(fā)送put和delete請求,則需要通過spring提供的過濾器HiddenHttpMethodFilter,在restful部分會講到。

到此這篇關(guān)于SpringMVC @RequestMapping注解詳解的文章就介紹到這了,更多相關(guān)SpringMVC @RequestMapping內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于java中可變長參數(shù)的定義及使用方法詳解
下面小編就為大家?guī)硪黄P(guān)于java中可變長參數(shù)的定義及使用方法詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
SpringBoot使用knife4j進行在線接口調(diào)試
這篇文章主要介紹了SpringBoot使用knife4j進行在線接口調(diào)試,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java Predicate及Consumer接口函數(shù)代碼實現(xiàn)解析
這篇文章主要介紹了Java Predicate及Consumer接口函數(shù)代碼實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
詳解Spring中Bean后置處理器(BeanPostProcessor)的使用
BeanPostProcessor 接口也被稱為Bean后置處理器,通過該接口可以自定義調(diào)用初始化前后執(zhí)行的操作方法。本文將詳細講講它的使用,需要的可以參考一下2022-06-06
Java實現(xiàn)無損Word轉(zhuǎn)PDF的示例代碼
本文將利用Java中的兩個jar包:pdfbox和aspose-words實現(xiàn)無損Word轉(zhuǎn)PDF功能,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下2022-06-06
SpringBoot前后端傳輸加密設(shè)計實現(xiàn)方案
這篇文章主要給大家介紹了關(guān)于SpringBoot前后端傳輸加密設(shè)計實現(xiàn)方案的相關(guān)資料,包括數(shù)據(jù)加密方案、解密傳輸數(shù)據(jù)實現(xiàn)方案和響應(yīng)數(shù)據(jù)加密實現(xiàn)方案,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-11-11
macOS上使用gperftools定位Java內(nèi)存泄漏問題及解決方案
這篇文章主要介紹了macOS上使用gperftools定位Java內(nèi)存泄漏問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

