译者注
原文
https://www.baeldung.com/spring-request-param
Demo
https://github.com/eugenp/tutorials/tree/master/spring-web-modules/spring-mvc-basics-5
一、前言
在本文中,我们将要学习Spring的@RequestParam
注解,和它的作用。
我们可以使用@RequestParam
,从请求的参数或文件中,去解析查询参数。
二、简单的映射
假设我们定义了一个请求地址/api/foos,它接收了一个名为id
的参数:
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam String id) {
return "ID: " + id;
}
在这个示例中,我们使用@RequestParam
来解析id
的查询参数。
一个简单的GET请求将会调用这个getFoos()
方法:
http://localhost:8080/spring-mvc-basics/api/foos?id=abc
----
ID: abc
接下来,我们看一下这个注解的参数,包括:
- 名称name
- 值value
- 必须required
- 默认值defaultValue
3. 指定请求参数名称
在前面的例子中,变量名称和参数名称是相同的。但有时我们需要让这二者不同,或者我们没有使用SpringBoot,此时我们可能需要在编译时的配置文件中做一些额外的操作,否则参数名称将不会进行二进制编码。
幸运的是,我们可以使用name属性来配置请求参数@RequestParam
的名称:
@PostMapping("/api/foos")
@ResponseBody
public String addFoo(@RequestParam(name = "id") String fooId, @RequestParam String name) {
return "ID: " + fooId + " Name: " + name;
}
我们还可以写成@RequestParam(value = “id”)
或者干脆写成@RequestParam(“id”)
。
4. 可选的请求参数
使用@RequestParam
注解的方法参数默认是必须的,这就一位置如果参数没有在请求中给出,就会发生错误:
GET /api/foos HTTP/1.1
-----
400 Bad Request
Required String parameter 'id' is not present
我们可以配置required = false
来实现可选功能optional:
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam(required = false) String id) {
return "ID: " + id;
}
此时,无论请求中有没有给出这个参数,都可以正确的解析到我们刚才写的方法上:
http://localhost:8080/spring-mvc-basics/api/foos?id=abc
----
ID: abc
如果请求中没有给出参数,获取到的是null,而不是出错:
http://localhost:8080/spring-mvc-basics/api/foos
----
ID: null
4.1 使用Java8的Optional
我们还可以用Optional去装饰这个参数:
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam Optional<String> id){
return "ID: " + id.orElseGet(() -> "not provided");
}
此时,我们就不需要再去设置required属性了。如果请求中没有给出参数的值,就会返回默认值。
http://localhost:8080/spring-mvc-basics/api/foos
----
ID: not provided
5. 请求参数的默认值
我们通过defaultValue
属性,为请求参数设置默认值:
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam(defaultValue = "test") String id) {
return "ID: " + id;
}
这样写的效果就类似required=false
,用户不必再去提供这个参数:
http://localhost:8080/spring-mvc-basics/api/foos
----
ID: test
也可以提供参数:
http://localhost:8080/spring-mvc-basics/api/foos?id=abc
----
ID: abc
需要注意的是,当我们设置默认值属性时,必须属性required
就已经设置为false
了。
6. 映射所有的参数
我们还可以用映射,实现在没有定义参数名称的情况下,使用多个参数:
@PostMapping("/api/foos")
@ResponseBody
public String updateFoos(@RequestParam Map<String,String> allParams) {
return "Parameters are " + allParams.entrySet();
}
上面的写法会返回请求的参数,如果发起请求就会:
curl -X POST -F 'name=abc' -F 'id=123' http://localhost:8080/spring-mvc-basics/api/foos
-----
Parameters are {[name=abc], [id=123]}
7. 映射多值参数
单一变量的请求参数可以包含多个值:
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam List<String> id) {
return "IDs are " + id;
}
并且SpringMVC会自动映射到一个逗号分隔的id参数:
http://localhost:8080/spring-mvc-basics/api/foos?id=1,2,3
----
IDs are [1,2,3]
或者一个分离的id列表:
http://localhost:8080/spring-mvc-basics/api/foos?id=1&id=2
----
IDs are [1,2]
8. 结论
在本文中,我们学习了如何使用@RequestParam
注解。
你可以在Github上找到本文中示例的完整代码。