「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>


分享到:


相關文章: