@RequestParam
主要用于将请求参数区域的数据映射到控制层方法的参数上,在 GET 与 POST 方法中都适用,两种请求方法对应前端两种不同的请求方式,下面是@RequestParam一种出现方式

我们需要知道@RequestParam注解主要有哪些参数
- value:请求中传入参数的名称,如果不设置后台接口的value值,则会默认为该变量名。比如上图中第一个参数如果不设置value=“page”,则前端传入的参数名必须为pageNum,否则在后台接口中pageNum将接收不到对应的数据
- required:该参数是否为必传项。默认是true,表示请求中一定要传入对应的参数,否则会报404错误,如果设置为false时,当请求中没有此参数,将会默认为null,而对于基本数据类型的变量,则必须有值,这时会抛出空指针异常。如果允许空值,则接口中变量需要使用包装类来声明。
- defaultValue:参数的默认值,如果请求中没有同名的参数时,该变量默认为此值。
GET 请求
问号传参
1 2 3 4 5 6 7
| axios.get("http://localhost:8081/boot/one?name=xxx&age=22") .then(function (res) { console.log(res) }) .catch(function (err) { console.log(err) })
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@GetMapping(value = "/boot/get1") public Result getParamByQ(String name, Integer age) { if (name != null && age != null) { return new Result(name + age, "请求成功", 100); } return new Result(null, "请求失败", -1); }
|
反斜杠传参
1 2 3 4 5 6 7
| axios.get("http://localhost:8081/boot/get2/xxx/22") .then(function (res) { console.log(res) }) .catch(function (err) { console.log(err) })
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@GetMapping(value = "/boot/get2/{name}/{age}") public Result getParamByX(@PathVariable String name, @PathVariable Integer age) { if (name != null && age != null) { return new Result(name + age, "请求成功", 100); } return new Result(null, "请求失败", -1); }
|
POST 请求
- 前端代码
需要引入Qs模块,前端控制台执行npm install Qs
1 2 3 4 5 6 7 8 9
| axios.post("http://localhost:8081/boot/post1", Qs.stringify({name: 'hqz', age: 22}), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function (res) { console.log(res) }).catch(function (err) { console.log(err) })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@PostMapping(value = "/boot/post1") public Result getDataByForm(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age) { if (name != null && age != null) { return new Result(name + age, "请求成功", 100); } return new Result(null, "请求失败", -1); }
|
1 2 3 4 5 6 7 8 9 10 11
| let fd = new FormData() fd.append("name", "xxx") fd.append("age", 22)
axios.post("http://localhost:8081/boot/post2", fd, { headers: {'Content-Type': 'multipart/form-data'} }).then(function (res) { console.log(res) }).catch(function (err) { console.log(err) })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@PostMapping(value = "/boot/post2") public Result getDataByFormData(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age) { if (name != null && age != null) { return new Result(name + age, "请求成功", 100); } return new Result(null, "请求失败", -1); }
|