I found that both of the property param of @requestMapping and @requestParam could realize binding method parameter to URL address ? So in general case can they be replaced by each other ?
-
This is explained here: https://stackoverflow.com/questions/31398498/diffrence-between-requestparam-and-requestmapping#:~:text=The%20%40RequestMapping%20annotation%20on%20the,the%20arguments%20in%20the%20method. – Umesh Sanwal Sep 13 '20 at 15:46
1 Answers
probably you should have elaborated your question with examples.
Anyhow, @RequestMapping params and @RequestParams are generally used for different purposes
Let's take the example of:https://stackoverflow.com/ questions/63871895
In this URI we can see two different handlers in a controller.
@RequestMapping(path = "/questions/{id}", method = RequestMethod.GET)
public Question getQuestion(@PathVariable int id) {
// returns a particular question
}
@RequestMapping(path = "/questions", method = RequestMethod.GET)
public List<Question> getQuestions() {
// returns all questions
}
Now here, Parameter mappings are considered as restrictions that are enforced. The primary path mapping (i.e. the specified URI value) still has to uniquely identify the target handler, with parameter mappings simply expressing preconditions for invoking the handler.
Now, let's see an example for @RequestParams: https://www.google.com/search?client=opera&q=stackoverflow&sourceid=opera&ie=UTF-8&oe=UTF-8
In this URL we can see one handler as:
@RequestMapping(path = "/search", method = RequestMethod.GET)
public List<Results> getResults(@RequestParam Map allRequestParams) {
// returns results based on query parameters
}
This will always call the same handler whether query params are provided or not. So @RequestParams is used to extract the query parameters from the URL.
So, generally, you can try to use @RequestMapping params in place of @RequestParams but it will have the effects explained in the above examples.

- 379
- 2
- 14