SpringMVC 中 @PathVariable、@RequestParam、@RequestBody的使用
文章目錄
一、正確理解 訪問請求、URI、URL、請求引數 四者的區別
一個完整的請求路徑:http://www.test.com/user/031267/view?username=zhangsan&age=20
請求URL (requestURL)= http://www.test.com/user/031267/view
請求URI (requestURI)= /user/031267/view
請求引數(queryString)= username=zhangsan&age=20
二、@PathVariable
對映 URL 繫結的佔位符
@PathVariable
可以將 URL 中 佔位符引數
方法的入參
中:
URL 中的 { xxx }
佔位符可以通過 @PathVariable("xxx")
繫結到操作方法的入參中。
請求路徑 : http://www.test.com/user/031267/view?username=zhangsan&age=20
請求URL : http://www.test.com/user/031267/view
@RequestMapping(value = "/user/{userid}/view") //佔位符 userid
public String view ( @PathVariable("userid") String userid) { //@PathVariable 中指定 userid
System.out.println("userid= "+userid); //此處可以獲取:userid= 031267
return SUCCESS;
}
帶佔位符的URL 是Spring3.0 新增的功能,該功能在SpringMVC 向REST目標挺進發展過程中具有里程碑的意義。
三、@RequestParam
獲取request請求引數中的值
請求路徑:http://www.test.com/user/query?username=zhangsan&age=20
請求引數:username=zhangsan&age=20
@RequestMapping(value = "/user/query")
public String query(@RequestParam(value="username") String username) {
System.out.println("username = " + username); //此處列印:username = zhangsan
return SUCCESS;
}
通過@RequestParam
獲取 不同請求對應的值:
.../query?username=zhangsan
獲取username 等於 zhangsan
.../query?username=
獲取username==""
.../query
獲取username==null
@RequestParam
預設必傳的,不能為null
如上面例子,請求路徑是 http://www.test.com/user/query?age=20
,程式一定會報錯的,
如果username不傳時, 就是null ,而 @RequestParam
預設是必傳的。
解決方法是
第1種: 請求路徑改為 http://www.test.com/user/query?username=&age=20
表示 username==""
,不為null;
第2種: required=false
,具體如下:
@RequestParam(value="username",required=false) String username
四、@PathVariable
與 @RequestParam
的區別
4.1、@PathVariable
:
請求路徑 : http://www.test.com/user/031267/view?username=zhangsan&age=20
請求URL : http://www.test.com/user/031267/view
@PathVariable
是 處理 URL 中的引數值
- 請求URL 是
不帶請求引數
的; @PathVariable("userid")
是 獲取訪 請求URL 的 變數值 ;- 而 變數值(如 031267)是 請求URL 的一部分。
4.2、@RequestParam
:
請求路徑 :http://www.test.com/user/031267/view?username=zhangsan&age=20
請求引數: username=zhangsan&age=20
@RequestParam
是 獲取請求引數的值,也就是?
後面的那些值 。
username 的值 是 zhangsan,age 的值 就是20
4.3、@PathVariable
和 @RequestParam
綜合使用
請求路徑 : http://www.test.com/user/031267/view?username=zhangsan&age=20
@RequestMapping(value = "/user/{userid}/view")
public String query(@PathVariable("userid") String userid,@RequestParam(value="username") String username ) {
System.out.println("userid= "+userid); //此處可以獲取:userid= 031267
System.out.println("username = " + username); //此處列印:username = zhangsan
return SUCCESS;
}
五、@RequestBody
獲取 request 請求體 body 的值
-
僅限用於 post 請求
-
因為一次請求只能有一個請求體body,所以方法引數中也只能有一個
@RequestBody
引數。但是,也可以把 請求體body的物件拆分開,用多個@RequestBody
引數來接收。 -
一般情況下,
@RequestBody
只處理Content-Type
是application/json
或者application/xml
。(a)、使用時,必須在請求頭中聲是
Content-Type
是application/json
或者application/xml
。
(b)、引數必須通過HttpEntity
傳遞。
(c)、SpringMVC 通過使用HandlerAdapter
配置的HttpMessageConverters
來解析HttpEntity
中的資料,然後繫結到相應的 bean上。 -
特殊情況,
@RequestBody
也可以處理Content-Type
型別為application/x-www-form-urlcoded
編碼的內容。@RequestBody
會將處理結果放到一個MultiValueMap<String,String>
中。
@RequestMapping(value="addUser.do",method=RequestMethod.POST,consumes="application/json")
@ResponseBody
public int addUser(@RequestBody UserVo vo){
System.out.println( vo.toString() );
return 1;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Store</title>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
function addUser() {
var name = $("#userName").val();
var password = $("#password").val();
var email = $("#email").val();
var user = {
userName: name,
password: password,
email: email
};
$.ajax({
type: "POST",
url: "user/addUser.do",
data:JSON.stringify(user),
contentType:'application/json;charset=utf-8', //預設值: "application/x-www-form-urlencoded"。傳送資訊至伺服器時內容編碼型別。
dataType: "json", //預期伺服器返回的資料型別
success: function (msg) {
console.log(msg)
}
});
}
</script>
</head>
<body>
Post 提交
<form action="" method="post">
userName:<input type="text" id ="userName" value="david"/><br>
password:<input type="text" id ="password" value="111111"/><br>
email:<input type="text" id ="email" value="[email protected]"/><br>
<input type="button" value="OK" onclick="addUser()"/>
</form>
</body>
</html>
當前臺介面使用 GET 或 POST 方式提交資料時,資料編碼格式由請求頭的 Content-Type
指定。
分為以下幾種情況:
application/x-www-form-urlencoded
,這種情況的資料@RequestParam
、@ModelAttribute
可以處理,@RequestBody
也可以處理。multipart/form-data
,@RequestBody
不能處理這種格式的資料。(form表單裡面有檔案上傳時,必須要指定enctype屬性值為multipart/form-data
,意思是以二進位制流的形式傳輸檔案。)application/json
、application/xml
等格式的資料,必須使用@RequestBody
來處理。