@PathVariable
當(dāng)使用@RequestMapping URI template 樣式映射時(shí),@PathVariable能使傳過來的參數(shù)綁定到路由上,這樣比較容易寫出restful api

demo1:單參? ? url :?http://127.0.0.1:8080/good/100
@RequestMapping("/test/{id}")
public int test(@PathVariable int id) {
? ? return id;
}
demo2:多參????url :?http://127.0.0.1:8080/test/10/100
@RequestMapping("/test/{type}/{id}")
public String test(@PathVariable long type, @PathVariable long id) {
? ? return type+","+id;
}
@RequestParam
接收的參數(shù)是來自HTTP請(qǐng)求體或請(qǐng)求url的QueryString中,使用該注解時(shí)Content-Type字段,需要為application/x-www-form-urlencoded,同時(shí),支持post和get

demo1:最簡單的,URL中的key與形參一致???url :?http://127.0.0.1:8080/test?id=100
@RequestMapping("/test")
public int test01(@RequestParam(required = true, defaultValue = "0") int id) {
? ? return id;
}
demo2:URL中的key與形參不一致????url :?http://127.0.0.1:8080/test?uid=100
@RequestMapping("/test")
public int test02(@RequestParam(value = "uid", defaultValue = "0") int id) {
? ? return id;
}
demo3:URL中一個(gè)key有多個(gè)值? ??url :?http://127.0.0.1:8080/test?id=100&id=200
@RequestMapping("/test")
public String test03(@RequestParam(value="id") long[] id){
? ? System.out.println(id);
? ? return "";
}
//或者是
@RequestMapping("/test")
public String test04(@RequestParam(value="list") List<Integer> list){
? ? System.out.println(list);
? ? return "";
}
demo4:一般使用map集合來接受多參? ??url :?http://127.0.0.1:8080/test?id=100&name=tom
@RequestMapping("/test")
public String test01(@RequestParam Map params){
????String name = params.get("name").toString();
????return name;
}
@RequestBody
該注解常用于接受請(qǐng)求體中非application/x-www-form-urlencoded編碼格式的數(shù)據(jù),比如:application/json、application/xml,現(xiàn)在前后端分離系統(tǒng)那么多,所以這才是主角。

總之
/{id}這種傳參形式可以用形式@PathVariable
url中的?后面參數(shù)可以用@RequestParam,?form-data、x-www-form-urlencoded可以用@RequestParam
application/json:json字符串可以用@RequestBody