Spring Boot @RequestParam元注释 | Java Development


在这篇文章中,我们将探索Spring的@RequestParam注释。@RequestParam注释结合web请求参数的控制器的方法。简单来说,我们可以使用 @RequestParam注释从查询参数和参数中获取值。让我们仔细看看一些重点:

@RequestParam映射
让我们创建一个简单的控制器来更好地理解这个注释:

@GetMapping("/greeting")
 public String sayHello(@RequestParam String name) {
  return
"Hello "+name+"!!!";
 }

在上面的示例中,我们使用@RequestParam注释提取查询参数。这是我们的请求的样子:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:8080/greeting?name=javadevjournal

参数名称
我们想要设置参数名称并且不想使用默认参数(请求参数的一部分)的情况很少。当我们想要在Id字段中存储电子邮件地址时,我们将采用一个简单的用例。

http://localhost:8080/getUser?email=contact-us@javadevjournal.com

@GetMapping("/getUser")
  public String getUser(@RequestParam(name =
"email") String id) {
   return
"It seems we have a record for email " + id;
  }

我们也可以@RequestParam(name =“email”)或@RequestParam(“email”)。

请求参数的默认值
此注释允许我们为请求参数设置默认值。这对于发送空参数的默认响应很有用。

@GetMapping("/default-value")
 public String defaultValueExample(@RequestParam(defaultValue =
"Anonymous user") String name) {
  return
"Hello " + name + "!!!";
 }

让我们看看以下请求的结果是什么:

http://localhost:8080/default-value
output: Hello Anonymous user!!!

让我们看看当我们发送名称作为请求的一部分时,这会如何反应

http://localhost:8080/default-value?name=Java Development Journal
Output: Hello Java Development Journal!!!

可选的请求参数
默认情况下,带注释的参数  @RequestParam 是必需的。这意味着客户端需要将信息作为请求的一部分传递,否则API将引发错误:

@GetMapping("/greeting")
 public String sayHello(@RequestParam String name) {
  return
"Hello "+name+"!!!";
 }

如果我们在请求中没有传递“名称”的情况下发送请求,我们将从服务中收到错误。

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:8080/greeting
HTTP/1.1 400 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 16 Dec 2018 01:39:21 GMT
Connection: close

{  
   
"timestamp":"2018-12-16T01:39:21.193+0000",
   
"status":400,
   
"error":"Bad Request",
   
"message":"Required String parameter 'name' is not present",
   
"path":"/greeting"
}

下面注释允许我们使用required属性将此参数标记为“ 可选 ” 。让我们更改上面的示例以查看此操作:

@GetMapping("/optional")
 public String optional(@RequestParam(required = false) String name) {
  return getGreeting(name);
 }

让我们看看控制器方法的响应是什么:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:8080/optional
HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Content-Length: 16
Date: Sun, 16 Dec 2018 01:46:33 GMT
Hello Stranger!!

多值参数
RequestParam注释 可以在列表中的多个值映射。让我们举个例子,我们希望将多个值作为逗号分隔值发送到方法,并将这些值存储为List。Spring MVC将映射列表中以逗号分隔的值。让我们通过一个例子来理解这个:

@GetMapping("/products")
public String getProducts(@RequestParam List < String > id) {
   return
"Products: " + id;
  }

以下是我们的请求输出:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:8080/products?id=12,13,14
HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Content-Length: 22
Date: Sun, 16 Dec 2018 02:01:46 GMT
Products: [12, 13, 14]
http:
//localhost:8080/products?id=12&id=13
Output: Products: [12, 13]


@RequestParam vs @PathVariable
我可以使用两个注释从请求URI中获取值。让我们来看看之间的区别@RequestParam和@PathVariable?

  • PathVariable 是从URI获取占位符。
  • @RequestParam 是从URI获取参数

这是@PathVariable注释的样子

@RequestMapping("/products/{code}")
public String getProduct(@PathVariable(value =
"code") String code,
 @RequestParam(value =
"category", required = false) String category) {
 .......
}