1.

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 */}


Discussion

No Comment Found