注解介绍
@RequestBody
通常用于POST
请求方式
@RequestParam
使用参数:@RequestParam(value = "name")
,但是@RequestParam
注解中的value
必须 和 前端传入参数的key
一致,自动匹配。
@RequestBody
和@RequestParam
可以组合使用。
@RequestBody
在每个方法中只能使用一次
,但@RequestParam
可以使用多次
@RequestBody
接收的是请求体里面的数据(application/json
);@RequestParam
接收的是key-value
里面的参数
GET请求
单参,无注解
1
| http://localhost:8080/demo/getData?name=111
|
1 2 3 4
| @RequestMapping(value = "/getData", method = RequestMethod.GET) public void getData(String name){ System.out.println(name); }
|
单参,@RequestParam
注意:@RequestParam(value = "name")
,入参必须与value
属性名一致;不加value
,则默认为Java方法中的接收参数名
例如:
1
| @RequestParam(value = "name1") String name
|
1
| http://localhost:8080/demo/getData?name=111
|
1 2 3 4
| @RequestMapping(value = "/getData", method = RequestMethod.GET) public void getData(@RequestParam String name){ System.out.println(name); }
|
多参,无注解
1
| http://localhost:8080/demo/getData?name1=111&name2=222
|
1 2 3 4
| @RequestMapping(value = "/getData", method = RequestMethod.GET) public void getData(String name1, String name2){ System.out.println(name1 + "," + name2); }
|
多参,@RequestParam
注意:@RequestParam(value = "name")
,入参必须与value
属性名一致;不加value
,则默认为Java方法中的接收参数名
例如:
1 2
| @RequestParam(value = "name1") String name
|
1
| http://localhost:8080/demo/getData?name1=111&name2=222
|
1 2 3 4
| @RequestMapping(value = "/getData", method = RequestMethod.GET) public void getData(@RequestParam String name1, @RequestParam String name2){ System.out.println(name1 + "," + name2); }
|
实体,无注解
1
| http://localhost:8080/demo/getData?id=1&name=zhangsan
|
1 2 3 4 5
| @RequestMapping(value = "/getData", method = RequestMethod.GET) public DemoEntity getData(DemoEntity params){ System.out.println(params.toString()); return params; }
|
实体,@RequestBody
1 2 3 4 5 6 7
| http://localhost:8080/demo/getData
Body入参: { "id":111, "name":"zhangsan" }
|
1 2 3 4
| @RequestMapping(value = "/getData", method = RequestMethod.GET) public void getData(@RequestBody DemoEntity params){ System.out.println(params.toString()); }
|
POST请求
单参,无注解
1
| http://localhost:8080/demo/addData?name=111
|
1 2 3 4
| @RequestMapping(value = "/addData", method = RequestMethod.POST) public void addData(String name){ System.out.println(name); }
|
单参,@RequestParam
注意:@RequestParam(value = "name")
,入参必须与value
属性名一致;不加value
,则默认为Java方法中的接收参数名
例如:
1 2
| @RequestParam(value = "name1") String name
|
1
| http://localhost:8080/demo/addData?name=111
|
1 2 3 4
| @RequestMapping(value = "/addData", method = RequestMethod.POST) public void addData(@RequestParam String name){ System.out.println(name); }
|
多参,无注解
1
| http://localhost:8080/demo/addData?name1=111&name2=222
|
1 2 3 4
| @RequestMapping(value = "/addData", method = RequestMethod.POST) public void addData(String name1, String name2){ System.out.println(name1 + "," + name2); }
|
多参,@RequestParam
注意:@RequestParam(value = "name")
,入参必须与value
属性名一致;不加value
,则默认为Java方法中的接收参数名
例如:
1 2
| @RequestParam(value = "name1") String name
|
1
| http://localhost:8080/demo/addData?name1=111&name2=222
|
1 2 3 4
| @RequestMapping(value = "/addData", method = RequestMethod.POST) public void addData(@RequestParam String name1, @RequestParam String name2){ System.out.println(name1 + "," + name2); }
|
实体,无注解
1
| http://localhost:8080/demo/addData?id=111&name=zhangsan
|
1 2 3 4
| @RequestMapping(value = "/addData", method = RequestMethod.POST) public void addData(DemoEntity params){ System.out.println(params.toString()); }
|
实体,@RequestBody
1 2 3 4
| @RequestMapping(value = "/addData", method = RequestMethod.POST) public void addData(@RequestBody DemoEntity params){ System.out.println(params.toString()); }
|
实体+文件,@RequestParam
+无注解
文件和参数一起提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @RequestMapping("/upload") public void upload(@RequestParam(value = "file") MultipartFile files, DemoEntity parmas){ System.out.println(params); String s = UploadUtils.uploadFile(files, filePath); }
后端Ajax写法 : ```JavaScript function submit(){ var file = $("#img")[0].files[0]; if (file){ var fileSize = file.size/(1024*1024); if(fileSize>1){ alert('图片大小最大不能超过1M!'); return false; } } var fd = new FormData(); fd.append("id", $("#id").val()) fd.append("name", $("#name").val()) fd.append("file", file) $.ajax({ type: "post", url: "http://localhost:8080/demo/upload", data: fd, contentType: false, processData: false, success: function (data,status) { } }) }
|