[英]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. I am building REST API using SpringBoot, this REST API accept more than one data from consumer like eg empId, empName, empDept. In my current code I am using
@RequestParam
annotation for accessing the query parameter values from the request.在我当前的代码中,我使用
@RequestParam
注释从请求中访问查询参数值。 But I see we can also use the @PathVariable
to get the data from placeholder of URI.但我看到我们也可以使用
@PathVariable
从 URI 的占位符中获取数据。
Wanted to know what is the best practice to get the multiple input request using @PathVariable
@RequestParam
OR something else?想知道使用
@PathVariable
@RequestParam
或其他东西获取多输入请求的最佳实践是什么?
Imagine this.想象一下。 In case you have a list of users:
如果您有用户列表:
GET /users (here it lists all your users)
It might happen for you to click on one user to access his details:您可能会点击一个用户来访问他的详细信息:
GET /users/{id}
This {id}
you use as a @PathVariable
您将这个
{id}
用作@PathVariable
You might want to use a @RequestParam
to filter users:您可能希望使用
@RequestParam
过滤用户:
GET /users?name=tst&age=21
And your GET /users have these requests param for you to filter:并且您的 GET /users 有这些请求参数供您过滤:
@RequestParam(value ="name")
@RequestParam(value ="age")
Usually, @PathVariable used in a case such as "getByAnything", for example, getUserById
and one Url can have only one placeholder variable(desirable).通常,@PathVariable 用于“getByAnything”等情况,例如
getUserById
和一个 Url 只能有一个占位符变量(理想)。 If your API has more than one variable your choice is @RequestParam.如果您的 API 有多个变量,您的选择是 @RequestParam。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.