本篇文章,用来探寻SpringBoot接收请求的多种方法。如果有些遗漏,或者有错误,还请各位指正。
首先定义一个User实体类:
@Data
class User {
String name;
int age;
User() {
}
User(String name, int age) {
this.name = name;
this.age = age;
}
}
一、接收get请求
(1)后端用实体类接收
@GetMapping("/loginByUser")
public User loginByUser(User user) {
return user;
}
前端则调用url:localhost:8080/user/loginByUser?name=tom&age=12
(2)后端用参数接收
@GetMapping("/loginByParam")
public User loginByParam(@RequestParam("name1") String name,
@RequestParam(value = "age", required = true, defaultValue = "20") int age) {
return new User(name, age);
}
前端则调用url:localhost:8080/user/loginByParam?name1=tom
@RequestParam注解将请求参数绑定到方法参数上。它有以下3个常用参数
- value:用来声明请求参数中的参数名称。例子中将请求参数中的name1绑定到方法参数中的name字段。
- required:当没声明其required时,默认是true。即如果前端没传入name1的话,后端则会报错。
- defaultValue:当age参数的required=true时,而前端又没有传入这个参数时,则参数列表中的这个age将会有一个默认值。
此时情况下的@RequestParam注解,可加可不加。
(3)后端使用Map接收
@GetMapping("/loginByMap")
public User loginByMap(@RequestParam Map<String, Object> map) {
String name = (String) map.get("name");
int age = Integer.parseInt((String) map.get("age"));
return new User(name, age);
}
前端则调用url:localhost:8080/user/loginByMap?name=tom&age=12
值得注意的是,这里的map参数前需要加@RequestParam注解,用于将请求参数注入到map中。
(4)后端用路径接收
@GetMapping("/loginByPath/{name}/{age}")
public User loginByPath(@PathVariable("name") String name, @PathVariable("age") int age) {
return new User(name, age);
}
前端则调用url:localhost:8080/user/loginByPath/tom/12
@PathVariable注解用于将路径中的参数绑定到方法参数中
二、接收Post请求
(1)后端使用实体类进行接收,前端传入Content-Type:application/json格式的数据
@PostMapping("/loginByUser")
public User loginByUser(@RequestBody User user) {
return user;
}
@RequestBody注解用于将请求体中的json字符串转化为java对象。
值得注意的是
- 由于get无请求体,那么@RequestBody不能使用在get请求上。
- @RequestBody与@RequestParam可以同时使用,@RequestBody最多只能有一个,而@RequestParam可以有多个。
(2)后端使用实体类进行接收,前端传入Content-Type:application/x-www-form-urlencoded格式的数据
@PostMapping("/loginByUser")
public User loginByUser(User user) {
return user;
}
Content-Type:application/x-www-form-urlencoded格式的数据,数据会以key/value格式进行传输,SpringMvc会直接将请求体中的参数直接注入到对象中。
(3)后端使用参数进行接收,前端传入Content-Type:application/x-www-form-urlencoded格式的数据
@PostMapping("/loginByParam")
public User loginByParam(@RequestParam("name1") String name,
@RequestParam(value = "age", required = true, defaultValue = "20") int age) {
return new User(name, age);
}
此时的@RequestParam注解加不加都无所谓
(4)后端使用Map来接收,前端传入Content-Type:application/x-www-form-urlencoded格式的数据
@PostMapping("/loginByMap")
public User loginByMap(@RequestParam Map<String, Object> map) {
String name = (String) map.get("name");
int age = Integer.parseInt((String) map.get("age"));
return new User(name, age);
}
这里类似于get请求的(3),同样,map参数前需要加@RequestParam注解,用于将请求参数注入到map中。
值得注意的是,由于form表单形式是以key/value形式存储,都是字符串类型,因此需要将map.get("age")转化为String,再转化为Integer,最后再自动拆箱。
不可以将map.get("age")直接转化为Integer类型,因为其本质是String类型,String不能直接强转为Integer。
(5)后端使用Map来接收,前端传入Content-Type:application/json格式的数据
@PostMapping("/loginByMap")
public User loginByMap(@RequestBody Map<String, Object> map) {
String name = (String) map.get("name");
int age = (Integer) map.get("age");
return new User(name, age);
}
这里类似于post请求的(1),同样,@RequestBody注解用于将请求体中的json字符串转化为对象属性,并注入到map中。
由于请求体中json中的age类型为number类型,因此注入到map中时,age是Integer类型,那么可以直接强转为Integer类型。
(6)后端使用JSONObject来接收,前端传入Content-Type:application/json格式的数据
@PostMapping("/loginByJSONObject")
public User loginByJSONObject(@RequestBody JSONObject jsonObject) {
String name = jsonObject.getString("name");
int age = jsonObject.getInteger("age");
return new User(name, age);
}
@RequestBody注解用于将请求体中的json字符串转化为JSON对象。
总结:
如果前端传入Content-Type:application/json格式的数据,直接使用@RequestBody注解将json字符串转化为对象。
如果前端传入Content-Type:application/x-www-form-urlencoded格式的数据,如果能够得出方法参数具有的属性和请求参数一样的属性时,则不需要@RequestParam注解。例如注入到Map中,则需要@RequestParam注解。
另外,get请求的请求头没有Content-Type字段。