springboot如何去获取前端传递的参数呢?
下文笔者讲述SpringBoot获取前端参数的方法及示例分享,如下所示
SpringBoot获取前端参数的实现思路
方式1: `@RequestParam`:用于获取 URL 查询参数或表单数据。 方式2: `@PathVariable`:用于获取 URL 路径变量。 方式3: `@RequestBody`:用于获取请求体中的 JSON 数据。 方式4: `@RequestHeader`:用于获取请求头中的参数。例:
1.获取URL查询参数
URL 查询参数是通过URL 的 `?`符号传递的键值对 例: `http://java265.com/api?name=John&age=18` 例 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/api") public String getUser(@RequestParam String name, @RequestParam int age) { return "Name: " + name + ", Age: " + age; } } 代码说明: - `@RequestParam` 注解用于从 URL 查询参数中获取值。 - `name` 和 `age` 是查询参数的名称。
2.获取路径变量
路径变量是URL路径的一部分 例: `http://java265com/api/user/123` import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/api/user/{id}") public String getUserById(@PathVariable Long id) { return "User ID: " + id; } } 代码说明 - `@PathVariable` 注解用于从 URL 路径中获取值。 - `{id}` 是路径变量的名称。
3.获取请求体中参数(POST 请求)
请求体中的参数通常用于 POST 请求 可以是表单数据或 JSON 数据。 表单数据 import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @PostMapping("/api/user") public String createUser(@RequestParam String name, @RequestParam int age) { return "Created User: " + name + ", Age: " + age; } }
JSON 数据
假设前端传递的 JSON 数据如下: json { "name": "John", "age": 18 } 例 import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @PostMapping("/api/user") public String createUser(@RequestBody User user) { return "Created User: " + user.getName() + ", Age: " + user.getAge(); } } class User { private String name; private int age; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 代码说明 - `@RequestBody` 注解用于从请求体中获取 JSON 数据并自动绑定到 Java 对象。 - `User` 类需要有相应的 getter 和 setter 方法。
4.获取请求头中参数
请求头中的参数 可通过 `@RequestHeader` 注解获取 例 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/api/user") public String getUser(@RequestHeader("Authorization") String authorization) { return "Authorization Header: " + authorization; } } 代码说明 `@RequestHeader`注解 用于从请求头中获取指定的头信息
5.获取所有请求参数
如果需要获取所有请求参数 可使用 `@RequestParam` 注解结合 `Map` 或 `MultiValueMap`。 例 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class UserController { @GetMapping("/api/user") public String getUser(@RequestParam Map<String, String> allParams) { return "All Params: " + allParams; } } 代码说明 -`@RequestParam` 注解 结合`Map`可以获取所有请求参数
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。