What are the differences between @RequestParam and @PathVariable?

Technology CommunityCategory: SpringWhat are the differences between @RequestParam and @PathVariable?
VietMX Staff asked 3 years ago

Even though both @RequestParam and  @PathVariable annotations are used to extract some data from URL, there is a key difference between them.

The @RequestParam is used to extract query parameters, e.g. anything after “?” in the URL while the @PathVariable is used to extract the part of the URI itself.

For example, if the given URL is

http: //localhost:8080/SpringMVC/books/3232233/?format=json

then you can access the query parameter “format” using the @RequestParam annotation and /books/{id} using the  @PathVariable, which will give you 3232233.