Question - What are the differences between @RequestParam and @PathVariable annotations?
Answer -
- Even though both these annotations are used to extract some data from URL, there is a key difference between them.
- The @RequestParam is used to extract query parameters that is anything after “?” in the URL.
- The @PathVariable is used to extract the data present as part of the URI itself.]
- For example, if the given URL is http://localhost:8080/InterviewBit/Spring/SpringMVC/?format=json, then you can access the query parameter “format” using the @RequestParam annotation and /Spring/{type} using the @PathVariable, which will give you SpringMVC.
@RequestMapping("/Spring/{type}")
public void getQuestions(@PathVariable("type") String type,
@RequestParam(value = "format", required = false) String format){
/* Some code */
}