0 votes
in Spring by
What are the differences between @RequestParam and @PathVariable annotations?

1 Answer

0 votes
by

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.

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

  }

Related questions

0 votes
asked Apr 4, 2021 in Spring by Robindeniel
0 votes
asked Apr 4, 2021 in Spring by Robindeniel
...