基本數據類型
java的基本數據類型有八個(byte,short,int,long,float,double,char,boolean),選擇以這些基本類型入參,不管是單個參數還是多個參數使用的方式方法都一樣,總之就是怎麼樣都是可以接受到的。
Content-Type:application/x-www-form-urlencoded
1、@PathVariable(get,post請求方式)
獲取單個或多個路徑參數。即url/{id}這種形式。而且還能進行正則驗證(@GetMapping("/testGet2_1/{param1:[0-9]{5}}/{param2}"))
2、@RequestParam(get,post請求方式)
既可以獲取查詢參數。即url?name=這種形式,也可以body體的數據
3,HttpServletRequest(get,post請求方式)
request.getParameter("name");
4,自動匹配
其實也就是直接列參數,springmvc會自動的綁定參數,這種方式簡單粗暴直接,使用的話與上面無異
數組/集合等類型
1,List
》@RequestBody
這種相當於是使用了json數組傳遞,因爲使用了@RequestBody這種就只能由post請求了
Content-Type:application/json
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>參數驗證</title>
</head>
<body>
<input type="button" name="request" value="請求後臺"
style="width:200px;height:50px;background-color:red;margin-bottom:20px;">
<div name="rs"></div>
<input type="checkbox" name="se" value="1">hafiz.zhang<br/>
<input type="checkbox" name="se" value="2">jack.chen<br/>
<input type="checkbox" name="se" value="3">lili.wang<br/>
<script src="/jquery-3.4.1.js"></script>
<script type="text/javascript">
$("input[name='request']").click(function () {
var data = [];
$("input[name='se']").each(function () {
if ($(this).prop("checked")) {
data.push($(this).val());
}
});
var json_data = JSON.stringify(data);
$.ajax({
type: "post",
url: "/test/test/param3",
contentType: "application/json",
data: json_data,
dataType: "json",
success: function (data) {
var str = "";
for (var i = 0; i < data.length; i++) {
str += ";name=" + data[i];
}
$("div[name='rs']").html(str);
},
error: function () {
alert("出錯啦");
}
});
});
</script>
</body>
</html>
@PostMapping("/test/param3")
@ResponseBody
public JsonResult testParam3(@RequestBody List<Integer> param){
System.out.println(param);
return JsonResult.success(param);
}
》@RequestParam()
使用@RequestParam()方式接收,請求方式get,post都可以,但是要注意Content-Type的差異,以及要注意數據不能爲空
get請求
Content-Type:application/x-www-form-urlencoded
Content-Type:application/json
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>參數驗證</title>
</head>
<body>
<input type="button" name="request" value="請求後臺"
style="width:200px;height:50px;background-color:red;margin-bottom:20px;">
<div name="rs"></div>
<input type="checkbox" name="se" value="1">hafiz.zhang<br/>
<input type="checkbox" name="se" value="2">jack.chen<br/>
<input type="checkbox" name="se" value="3">lili.wang<br/>
<script src="/jquery-3.4.1.js"></script>
<script type="text/javascript">
$("input[name='request']").click(function () {
var data = [];
$("input[name='se']").each(function () {
if ($(this).prop("checked")) {
data.push($(this).val());
}
});
// var json_data = JSON.stringify(data);
$.ajax({
type: "get",
url: "/test/test/param4",
// contentType: "application/json",
data: {"datas": data},
dataType: "json",
success: function (data) {
var str = "";
for (var i = 0; i < data.length; i++) {
str += ";name=" + data[i];
}
$("div[name='rs']").html(str);
},
error: function () {
alert("出錯啦");
}
});
});
</script>
</body>
</html>
@PostMapping("/test/param4")
@ResponseBody
public JsonResult testParam4(@RequestParam("datas[]") List<Integer> param){
System.out.println(param);
return JsonResult.success(param);
}
post請求
Content-Type:application/x-www-form-urlencoded
代碼如上get請求一樣,只需將get請求換成post請求。如果想探索一下的話,可以試試Content-Type的不同,我的驗證是
Content-Type:application/x-www-form-urlencoded 沒有問題
Content-Type:application/json 請求異常(原因是application/json要求的是json字符串,而我們給的數據是json數據,所以後臺報匹配不到數據。多說一句,post請求下既然我們使用@RequestParam()這個註解,自然就不會使用application/json)
》請求參數使用join()處理
這種處理方式簡單粗暴,且不用預防參數爲空而導致的異常,請求方式也都可以
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>參數驗證</title>
</head>
<body>
<input type="button" name="request" value="請求後臺"
style="width:200px;height:50px;background-color:red;margin-bottom:20px;">
<div name="rs"></div>
<input type="checkbox" name="se" value="1">hafiz.zhang<br/>
<input type="checkbox" name="se" value="2">jack.chen<br/>
<input type="checkbox" name="se" value="3">lili.wang<br/>
<script src="/jquery-3.4.1.js"></script>
<script type="text/javascript">
$("input[name='request']").click(function () {
var data = [];
$("input[name='se']").each(function () {
if ($(this).prop("checked")) {
data.push($(this).val());
}
});
console.log(data.join());
// var json_data = JSON.stringify(data);
$.ajax({
type: "get",
url: "/test/test/param5",
// contentType: "application/json",
data: {"datas": data.join()},
dataType: "json",
success: function (data) {
var str = "";
for (var i = 0; i < data.length; i++) {
str += ";name=" + data[i];
}
$("div[name='rs']").html(str);
},
error: function () {
alert("出錯啦");
}
});
});
</script>
</body>
</html>
@GetMapping("/test/param5")
@ResponseBody
public JsonResult testParam5(@RequestParam("datas") List<Integer> param){
System.out.println(param);
return JsonResult.success(param);
}
2,數組
如上所示,只需將List類型改爲數組類型即可
3,Map
使用map接收參數好在靈活多變,但是優點也是缺點,靈活多變意味着類型不確定,需要多做一些操作,所以除非需求使然,否則不建議用此類型接收。
1,@RequestParam
@PostMapping("/test/param6")
@ResponseBody
public JsonResult testParam6(@RequestParamMap<String,Object> param){
System.out.println(param);
return JsonResult.success(param);
}
2,@RequestBody
@PostMapping("/test/param6")
@ResponseBody
public JsonResult testParam6(@RequestBody Map<String,Object> param){
System.out.println(param);
return JsonResult.success(param);
}
對象類型
1,簡單對象
Content-Type:application/x-www-form-urlencoded
springmvc會自動進行裝箱,接收的是json
@PostMapping("/testPost4")
public String testPost4(ParamsEntity paramsEntity){
System.out.println("params:"+paramsEntity.toString());
return paramsEntity.toString();
}
// post請求
function testPost4() {
var data = {};
data.param1 = "123";
data.param2 = "2233";
$.post(basePath + "/api/params/testPost4", data, function (data) {
alert("SpringMVC自動裝箱---後臺返回來的參數:" + data);
});
}
// get請求
function testGet4() {
var data = {};
data.param1 = "123";
data.param2 = "2233";
$.get(basePath + "/api/params/testGet4", data, function (data) {
alert("SpringMVC自動裝箱---後臺返回來的參數:" + data);
});
}
2,複雜對象
Content-Type:application/json
複雜的對象參數接收,我們一般考慮使用json進行傳輸
1、@RequestBody接收復雜對象
使用該註解即可接收復雜的java對象,也就是複雜的json格式的字符串
2,@RequestBody接收數組/List
這種就相當於就是json數組字符串傳遞
@PostMapping("/test/param7")
@ResponseBody
public JsonResult testParam7(@RequestBody List<TestParam> param){
System.out.println(param);
return JsonResult.success(param);
}
其他
1,請求頭參數
@RequestHeader
2,cookie參數
@CookieValue
補充
額外知識1:Http協議常用的四種請求方式:Post、Get、Put、Delete等;其中Put、Delete請求方式很少見,都可用Post方式代替!
a)對數據庫而言: get 請求不修改數據庫,只是查詢。Post是增加記錄,put是更新,Delete數據庫刪除;
b)Put,Post,Delete 方式的請求參數會直接放在requestBody裏;
c)處理 request uri 部分的註解,路徑參數變量:@PathVariable;
d)處理request header部分的註解: @RequestHeader, @CookieValue,@RequestParam;
e)處理request body部分的註解:@RequestParam, @RequestBody;
綜上所述:@RequestParam註解既可以接收Get方式的請求頭中的參數,也可以接收Post方式的請求體中的參數;
額外知識2:get請求的 headers 中沒有 content-type 這個字段,post 的 content-type 有 :
a)application/x-www-form-urlencoded 這種就是一般的文本表單用 post 傳地數據,只要將得到的 data 用 @RequestParam 或 request.getParamter() 獲取即可;
b)multipart/form-data ,用於文件上傳,此時 form 的 enctype 屬性必須指定爲 multipart/form-data;
c)application/json,將數據以json對象的格式傳遞;
d)text/xml;
e)put 和 delete 請求的headers 是有 content-type 這個字段的,只不過這兩個方法類型目前不常用;
額外知識3:
contentType的默認值是application/x-www-form-urlencoded
jQuery的ajax會默認把data:{"id":xxx,"name":xxx}這樣格式的數據拼接成id=xxx&name=xxx這種以表單數據格式提交的字符串