0

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?

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.