介绍:

Spring @RequestParam批注可用于在处理程序方法中提取查询参数。 在本快速教程中,我们将学习其用法。

首先让我们展示一个API,该API返回具有给定名字和年龄的用户列表:

@RestController
public class UserController {...@GetMapping("/users")public List<User> getUsers(@RequestParam String firstName, @RequestParam int age) {return userService.findUsersWith(firstName, age);}
}

现在,让我们使用cURL快速测试一下:

curl -XGET 'http://localhost:8080/users?firstName=John&age=15'
...
[{firstName = John ,lastName = Mason ,age = 15}, {firstName = John ,lastName = Dash ,age = 15}]

如我们所见, firstNameage是正确映射到我们的处理程序方法的查询参数。

@RequestParam批注支持使用多种属性来满足各种常见需求:

1.

在刚刚介绍的示例中,我们将注意到方法参数和查询参数的名称相同( firstNameage )。

但是, 有时,我们可能会觉得需要使用不同的名称。 为此,我们将使用其namevalue属性

@GetMapping("/users")
public List<User> getUsers(@RequestParam(name="uName") String firstName, @RequestParam("uAge") int age) {return userService.findUsersWith(firstName, age);
}

这样,UNAMEuAge参数将分别映射到firstName年龄的方法参数:

curl -XGET 'http://localhost:8080/users?uName=John&uAge=15'

2.

默认情况下, 如果我们定义了一个请求参数,并且未在用户请求中传递它,则会收到错误消息。

为了避免这种情况,我们可以将required设置为false:

@GetMapping("/users")
public List<User> getUsers(@RequestParam(required=false) String firstName, @RequestParam int age) {return userService.findUsersWith(firstName, age);
}

它将为所有可选参数分配默认数据类型。 当我们点击以下URL时:

http://localhost:8080/users?age=15

firstName将被映射为值。

3.

如果我们想要将required设置为false并且还将默认值映射到所关注的属性,我们将具有:

@GetMapping("/users")
public List<User> getUsers(@RequestParam(defaultValue="John") String firstName,@RequestParam int age) { return userService.findUsersWith(firstName, age);
}

在这里,如果我们不传递firstName查询参数,它将假定它是'John'

我们可以在Java 列表中接受查询参数的列表:

@GetMapping("/users/v2")
public List<User> getUsersWithGivenAges(@RequestParam List<Integer> age) {return userService.findUsersWithAges(age);
}

要使用我们的新用户API,我们将提供以下内容:

curl -XGET 'http://localhost:8080/users/v2?age=10,15,20'Orcurl -XGET 'http://localhost:8080/users/v2?age=10&age=15&age=20'

检索所有参数:

要从用户请求中检索所有传递的查询参数,我们将其接受为Java Map

@GetMapping("/paramsEx")
public Map<String, String> printParams(@RequestParam Map<String, String> allQueryParams) {System.out.println(allQueryParams); return allQueryParams;
}

当我们想检索所有参数而不知道它们的名称时,这很方便:

curl -XGET 'http://localhost:8080/paramsEx?param1=A&param2=B&param3=2'
...
{param1=A, param2=B, param3=2}

结论:

在本教程中,我们学习了如何在Spring应用程序中使用@RequestParam批注。

翻译自: https://www.javacodegeeks.com/2019/10/spring-requestparam-annotation.html

Spring @RequestParam批注相关推荐

  1. Spring @Order批注

    介绍: Spring @Order注释是在Spring 2.0中首次引入的. 然后,它仅用于定义AspectJ建议中的顺序. 在Spring 4.0的后面,对该注释的实现进行了进一步改进. 从那时起, ...

  2. Spring @Value批注

    介绍: Spring @Value批注用于将值注入变量和方法参数. 我们可以读取spring环境变量或系统变量. 它还支持SpEL. 在本快速教程中,我们将探讨如何使用Spring @Value批注. ...

  3. Spring @Service批注

    Spring @Service annotation is a specialization of @Component annotation. Spring Service annotation c ...

  4. Spring @Autowired批注

    Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built o ...

  5. Spring @Repository批注

    Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, ...

  6. Spring @Lazy批注用例

    Spring框架几乎可以毫不费力地为您解决许多常见的编程问题,但是它的某些功能比其他功能鲜为人知. 在本文中,我们将仔细研究属于该组的@Lazy批注. 阅读了几个示例之后,您应该能够将注释应用于日常开 ...

  7. Spring Enable批注–编写自定义的Enable批注

    Spring提供了一系列名称以Enable *开头的注释,这些注释本质上使某些Spring管理的功能可以被激活. 这样的注释的一个很好的例子是EnableWebMvc ,它引入了在基于Spring的应 ...

  8. Spring MVC中@RequestParam和@PathVariable批注之间的区别?

    Spring MVC框架是在Java世界中开发Web应用程序最流行的框架之一,它还提供了一些有用的注释,可以从传入的请求中提取数据并将请求映射到控制器,例如@ RequestMapping,@ Req ...

  9. 如何在Spring中将@RequestParam绑定到对象

    您是否在请求映射方法中用@RequestParam注释了多个参数,并认为它不可读? 当请求中需要一个或两个输入参数时,注释看起来非常简单,但是当列表变长时,您可能会感到不知所措. 您不能在对象内部使用 ...

最新文章

  1. 信息系统项目管理师长篇备考经验
  2. 五行中的土在哪个方位_土命人适合往哪个方向发展
  3. [转]vue-codemirror 代码编辑器
  4. UIActivityIndicatorView、UIProgressView 活动与进度指示器 (实例)
  5. js进阶 13 jquery动画函数有哪些
  6. matlab ode45三体问题,“毕达哥拉斯3Body Proxblem”ODE解算器测试的下一步
  7. mes系统服务器连接失败,mes系统如何连接其他系统设备?
  8. Lintcode 尾部的0
  9. iMX6 SoloX千兆以太网Linux PHY驱动调试
  10. 移动端H5游戏开发之(移动端尺寸基础知识)
  11. 25个常用Matplotlib图的Python代码,干货收藏!
  12. 【系统分析师之路】第二十一章 复盘系分专业英语
  13. 看看最新BTA大厂的Java程序员的招聘技术标准,Java篇
  14. mac远程桌面windows
  15. ubuntu 18.04 安装conda环境 及 创建虚拟环境
  16. 程序员必知必会网络传输之TCP/IP协议族,共864页的详解文档让你原地起飞!
  17. RHCSA之Linux系统简介
  18. 按了锁定计算机,鼠标锁定了按什么键解锁
  19. unity 构建迷宫_教程:使用GameDraw在Unity中构建迷宫游戏关卡
  20. Ubuntu安装GVM-11并使用gvm-tools命令行方式通讯

热门文章

  1. jzoj1274-游历的路线【分层图,SPFA】
  2. jzoj4231-寻找神格【线段树,数学】
  3. codeforces1553 F. Pairwise Modulo(数学)
  4. codeforces1471 D. Strange Definition
  5. Java 证书pem转KeyStore、jks文件
  6. 淘宝秒杀系统设计的几个注意点
  7. Unicode与UTF-8的区别
  8. Java中的函数传递
  9. java中的Queue队列的用法
  10. Hibernate中使用Criteria查询及注解——(Dept.hbm.xml)