What’s the difference between @PathVariable, @PathParam, and @RequestParam?

Even though @PathVariable and @RequestParam are both used to extract values from the URL, their usage is largely determined by how a site is designed.
The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters.
If we had chosen to use query parameters, our URL would be:
http://localhost:8080/orders?id=100
This would be implemented in a controller method like the following:
@GetMapping("/orders")
@ResponseBody
public String getOrder(@RequestParam(value = "id", required = true) String id) {
  return "Order ID: " + id;
}
These annotations can be mixed together inside the same controller.
@PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.   

Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance