user1591156
user1591156

Reputation: 1995

SpringBoot Rest API Get Method

I am building REST API using SpringBoot, this REST API accept more than one data from consumer like e.g. empId, empName, empDept. In my current code I am using @RequestParam annotation for accessing the query parameter values from the request. But I see we can also use the @PathVariable to get the data from placeholder of URI.

Wanted to know what is the best practice to get the multiple input request using @PathVariable @RequestParam OR something else?

Upvotes: 0

Views: 1125

Answers (2)

DaviM
DaviM

Reputation: 324

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

You might want to use a @RequestParam to filter users:

GET /users?name=tst&age=21

And your GET /users have these requests param for you to filter:

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

Upvotes: 1

Dmitrii B
Dmitrii B

Reputation: 2860

Usually, @PathVariable used in a case such as "getByAnything", for example, getUserById and one Url can have only one placeholder variable(desirable). If your API has more than one variable your choice is @RequestParam.

Upvotes: 0

Related Questions