繁体   English   中英

SpringBoot Rest API 获取方法

[英]SpringBoot Rest API Get Method

I am building REST API using SpringBoot, this REST API accept more than one data from consumer like eg empId, empName, empDept. 在我当前的代码中,我使用@RequestParam注释从请求中访问查询参数值。 但我看到我们也可以使用@PathVariable从 URI 的占位符中获取数据。

想知道使用@PathVariable @RequestParam或其他东西获取多输入请求的最佳实践是什么?

想象一下。 如果您有用户列表:

GET /users (here it lists all your users)

您可能会点击一个用户来访问他的详细信息:

GET /users/{id}

您将这个{id}用作@PathVariable

您可能希望使用@RequestParam过滤用户:

GET /users?name=tst&age=21

并且您的 GET /users 有这些请求参数供您过滤:

@RequestParam(value ="name")
@RequestParam(value ="age")

通常,@PathVariable 用于“getByAnything”等情况,例如getUserById和一个 Url 只能有一个占位符变量(理想)。 如果您的 API 有多个变量,您的选择是 @RequestParam。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM