本文將從以下多個方面對SpringBoot Get方式請求傳參做詳細的闡述,包括URL傳參、路徑傳參、請求頭傳參、請求體傳參等,幫助讀者更加深入地了解Get請求方式下傳參的相關知識。
一、URL傳參
在Get請求方式中,URL傳參是最為常見的一種方式,可以通過URL中添加參數的方式進行傳遞。例如:
http://localhost:8080/user?id=123&name=張三
在Controller中通過@RequestParam註解接收參數:
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(@RequestParam("id") Integer id, @RequestParam("name") String name) {
return "id: " + id + ", name: " + name;
}
}
通過@RequestParam註解可以指定參數的名稱,如果未指定,則默認使用參數名作為名稱,例如上面的name參數。
如果參數為非必選參數,可以使用required屬性將其設置為非必選參數:
@GetMapping("/user")
public String getUser(@RequestParam(value = "id", required = false) Integer id,
@RequestParam(value = "name", required = false) String name) {
return "id: " + id + ", name: " + name;
}
此時如果請求中不帶有id或者name參數,也不會拋出異常。
二、路徑傳參
除了URL傳參外,Get請求方式還可以通過路徑進行參數傳遞,通常用於RESTful API中:
http://localhost:8080/user/123
在Controller中通過@PathVariable註解接收參數:
@RestController
public class UserController {
@GetMapping("/user/{id}")
public String getUserById(@PathVariable("id") Integer id) {
return "id: " + id;
}
}
通過@PathVariable註解可以將路徑中的參數獲取到,並作為方法的參數使用。
如果路徑中有多個參數需要傳遞,可以按照以下方式進行定義:
@RestController
public class UserController {
@GetMapping("/user/{id}/name/{name}")
public String getUserByIdAndName(@PathVariable("id") Integer id, @PathVariable("name") String name) {
return "id: " + id + ", name: " + name;
}
}
三、請求頭傳參
在Get請求方式中,除了URL和路徑傳參外,還可以通過請求頭進行參數傳遞,可以增加信息的安全性和隱私性。例如:
http://localhost:8080/user
X-Token: abc
X-User-Id: 12345
在Controller中通過@RequestHeader註解接收參數:
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(@RequestHeader("X-Token") String token, @RequestHeader("X-User-Id") Integer userId) {
return "token: " + token + ", userId: " + userId;
}
}
通過@RequestHeader註解可以獲取請求頭中指定的參數,並作為方法的參數使用。
四、請求體傳參
除了以上三種傳參方式,還可以在Get請求方式中使用請求體進行參數傳遞。但是需要特別注意,Get請求方式中,並不是所有的瀏覽器都支持請求體傳參。這種方式一般用於前端使用ajax等技術進行請求,在後端Controller中通過@RequestBody註解接收參數。
$.ajax({
url: "/user",
type: "GET",
contentType: "application/json",
data: JSON.Stringify({id: 123, name: "張三"}),
success: function(result){
console.log(result);
}
});
在Controller中通過@RequestBody註解接收參數:
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(@RequestBody User user) {
return "id: " + user.getId() + ", name: " + user.getName();
}
}
需要注意的是,由於Get請求並不是通過請求體進行傳參的標準方式,因此需要使用contentType指定請求類型為application/json,同時在請求數據中需要將參數序列化為JSON字符串。
代碼示例:
1、URL傳參
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(@RequestParam("id") Integer id, @RequestParam("name") String name) {
return "id: " + id + ", name: " + name;
}
}
2、路徑傳參
@RestController
public class UserController {
@GetMapping("/user/{id}")
public String getUserById(@PathVariable("id") Integer id) {
return "id: " + id;
}
}
3、請求頭傳參
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(@RequestHeader("X-Token") String token, @RequestHeader("X-User-Id") Integer userId) {
return "token: " + token + ", userId: " + userId;
}
}
4、請求體傳參
前端發送請求:
$.ajax({
url: "/user",
type: "GET",
contentType: "application/json",
data: JSON.Stringify({id: 123, name: "張三"}),
success: function(result){
console.log(result);
}
});
在Controller中使用@RequestBody註解接收參數:
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(@RequestBody User user) {
return "id: " + user.getId() + ", name: " + user.getName();
}
}
以上為四種Get請求方式下的參數傳遞方式,開發者可以根據具體業務場景進行選擇,更加深入的了解和使用SpringBoot框架,可以讓我們的開發效率更加高效。
原創文章,作者:CORLT,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/374162.html