
在 Java Web 开发中,Spring 框架是一个非常流行的选择。当我们构建 Web 应用时,经常需要从客户端接收请求参数,Spring 提供了多种方式来处理这些请求参数。本文将详细介绍 Spring 中控制器接收请求参数的各种方法,并给出相应的演示代码。
@RequestParam 注解@RequestParam 注解用于从请求的查询字符串或表单数据中获取参数。
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class BasicParamController {@RequestMapping("/basicParam")@ResponseBodypublic String handleBasicParam(@RequestParam("name") String name, @RequestParam("age") int age) {return "Name: " + name + ", Age: " + age;}}
@RequestParam("name") 表示从请求中获取名为 name 的参数,并将其赋值给方法参数 name。@RequestParam("age") 同理,获取名为 age 的参数并赋值给 age。@RequestParam 的可选属性| 属性名 | 描述 |
|---|---|
value |
指定请求参数的名称,与 name 作用相同。 |
name |
指定请求参数的名称。 |
required |
表示该参数是否必需,默认为 true。 |
defaultValue |
当请求中没有该参数时,使用的默认值。 |
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class OptionalParamController {@RequestMapping("/optionalParam")@ResponseBodypublic String handleOptionalParam(@RequestParam(name = "name", required = false, defaultValue = "Guest") String name) {return "Hello, " + name;}}
当请求参数与实体类的属性名相同时,Spring 可以自动将请求参数绑定到实体对象上。
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;class User {private String name;private int age;// Getters and Setterspublic 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;}}@Controllerpublic class EntityParamController {@RequestMapping("/entityParam")@ResponseBodypublic String handleEntityParam(User user) {return "Name: " + user.getName() + ", Age: " + user.getAge();}}
name 和 age 会自动绑定到 User 对象的对应属性上。@PathVariable 注解@PathVariable 注解用于从请求的 URL 路径中获取变量。
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class PathVariableController {@RequestMapping("/user/{id}")@ResponseBodypublic String handlePathVariable(@PathVariable("id") int userId) {return "User ID: " + userId;}}
{id} 表示路径变量,@PathVariable("id") 用于获取该变量的值并赋值给 userId。@RequestBody 注解当客户端发送 JSON 数据时,我们可以使用 @RequestBody 注解将 JSON 数据转换为 Java 对象。
import org.springframework.http.MediaType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class JsonParamController {@RequestMapping(value = "/jsonParam", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)@ResponseBodypublic String handleJsonParam(@RequestBody User user) {return "Name: " + user.getName() + ", Age: " + user.getAge();}}
@RequestBody 注解将请求体中的 JSON 数据转换为 User 对象。consumes = MediaType.APPLICATION_JSON_VALUE 表示该请求只接受 JSON 格式的数据。Spring 提供了多种方式来接收请求参数,每种方式都有其适用场景:
| 接收方式 | 注解 | 适用场景 |
| —— | —— | —— |
| 基本请求参数 | @RequestParam | 从查询字符串或表单数据中获取单个参数。 |
| 实体对象参数 | 无(自动绑定) | 当请求参数与实体类属性名相同时,自动绑定到实体对象。 |
| 路径变量参数 | @PathVariable | 从请求的 URL 路径中获取变量。 |
| JSON 数据 | @RequestBody | 接收客户端发送的 JSON 数据并转换为 Java 对象。 |
通过合理使用这些方法,我们可以方便地处理各种类型的请求参数,构建出功能强大的 Web 应用。