「SpringBoot」SpringBoot接收请求的n种姿势

本篇文章,用来探寻SpringBoot接收请求的多种方法。如果有些遗漏,或者有错误,还请各位指正。


「SpringBoot」SpringBoot接收请求的n种姿势


首先定义一个User实体类:

<code>@Data
class User {
String name;
int age;

User() {
}

User(String name, int age) {
this.name = name;
this.age = age;
}
}/<code>

一、接收get请求

(1)后端用实体类接收

<code>@GetMapping("/loginByUser")
public User loginByUser(User user) {
return user;
}/<code>

前端则调用url:localhost:8080/user/loginByUser?name=tom&age=12

(2)后端用参数接收

<code>@GetMapping("/loginByParam")
public User loginByParam(@RequestParam("name1") String name,
@RequestParam(value = "age", required = true, defaultValue = "20") int age) {
return new User(name, age);
}/<code>

前端则调用url:localhost:8080/user/loginByParam?name1=tom

@RequestParam注解将请求参数绑定到方法参数上。它有以下3个常用参数

value:用来声明请求参数中的参数名称。例子中将请求参数中的name1绑定到方法参数中的name字段。

required:当没声明其required时,默认是true。即如果前端没传入name1的话,后端则会报错。

defaultValue:当age参数的required=true时,而前端又没有传入这个参数时,则参数列表中的这个age将会有一个默认值。

此时情况下的@RequestParam注解,可加可不加。

(3)后端使用Map接收

<code> @GetMapping("/loginByMap")
public User loginByMap(@RequestParam Map<string> map) {
String name = (String) map.get("name");
int age = Integer.parseInt((String) map.get("age"));
return new User(name, age);
}/<string>/<code>

前端则调用url:localhost:8080/user/loginByMap?name=tom&age=12

值得注意的是,这里的map参数前需要加@RequestParam注解,用于将请求参数注入到map中。

(4)后端用路径接收

<code>@GetMapping("/loginByPath/{name}/{age}")
public User loginByPath(@PathVariable("name") String name, @PathVariable("age") int age) {
return new User(name, age);

}/<code>

前端则调用url:localhost:8080/user/loginByPath/tom/12

@PathVariable注解用于将路径中的参数绑定到方法参数中

(5)后端用数组接收

<code> @GetMapping("/array")
public Integer[] array(Integer[] a) {
return a;
}/<code>

前端则调用url:localhost:8080/user/array?a=1&a=2&a=3

当然,这里也可用List<integer>来接收,不过需要加上@RequestParam("a")注解/<integer>

如果直接使用List<integer>来接收,也不加上@RequestParam("a")注解的话,则会报错/<integer>

No primary or default constructor found for interface java.util.List

二、接收Post请求

(1)后端使用实体类进行接收,前端传入Content-Type:application/json格式的数据

<code>@PostMapping("/loginByUser")
public User loginByUser(@RequestBody User user) {
return user;
}/<code>

@RequestBody注解用于将请求体中的json字符串转化为java对象。

值得注意的是

由于get无请求体,那么@RequestBody不能使用在get请求上。

@RequestBody与@RequestParam可以同时使用,@RequestBody最多只能有一个,而@RequestParam可以有多个。

(2)后端使用实体类进行接收,前端传入Content-Type:application/x-www-form-urlencoded格式的数据

<code>@PostMapping("/loginByUser")
public User loginByUser(User user) {
return user;
}/<code>

Content-Type:application/x-www-form-urlencoded格式的数据,数据会以key/value格式进行传输,SpringMvc会直接将请求体中的参数直接注入到对象中。

(3)后端使用参数进行接收,前端传入Content-Type:application/x-www-form-urlencoded格式的数据

<code> @PostMapping("/loginByParam")
public User loginByParam(@RequestParam("name1") String name,
@RequestParam(value = "age", required = true, defaultValue = "20") int age) {
return new User(name, age);
}/<code>

此时的@RequestParam注解加不加都无所谓

(4)后端使用Map来接收,前端传入Content-Type:application/x-www-form-urlencoded格式的数据

<code>@PostMapping("/loginByMap")
public User loginByMap(@RequestParam Map<string> map) {
String name = (String) map.get("name");

int age = Integer.parseInt((String) map.get("age"));
return new User(name, age);
}/<string>/<code>

这里类似于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格式的数据

<code> @PostMapping("/loginByMap")
public User loginByMap(@RequestBody Map<string> map) {
String name = (String) map.get("name");
int age = (Integer) map.get("age");
return new User(name, age);
}/<string>/<code>

这里类似于post请求的(1),同样,@RequestBody注解用于将请求体中的json字符串转化为对象属性,并注入到map中。

由于请求体中json中的age类型为number类型,因此注入到map中时,age是Integer类型,那么可以直接强转为Integer类型。

(6)后端使用JSONObject来接收,前端传入Content-Type:application/json格式的数据

<code>@PostMapping("/loginByJSONObject")
public User loginByJSONObject(@RequestBody JSONObject jsonObject) {
String name = jsonObject.getString("name");
int age = jsonObject.getInteger("age");
return new User(name, age);
}/<code>

@RequestBody注解用于将请求体中的json字符串转化为JSON对象。

(7)后端使用数组来接收

<code> @PostMapping("/array")
public Integer[] array(Integer[] a) {
return a;
}/<code>


分享到:


相關文章: