What is the difference between @PathParam and @PathVariable
@PathParam
is a parameter annotation which allows you to map variable URI path fragments into your method call.
@PathVariable
is to obtain some placeholder from the URI (Spring call it an URI Template)
QueryParam:
To assign URI parameter values to method arguments. In Spring, it is @RequestParam
.
Eg.,
http://localhost:8080/books?isbn=1234
@GetMapping("/books/")
public Book getBookDetails(@RequestParam("isbn") String isbn) {
PathParam:
To assign URI placeholder values to method arguments. In Spring, it is @PathVariable
.
Eg.,
http://localhost:8080/books/1234
@GetMapping("/books/{isbn}")
public Book getBook(@PathVariable("isbn") String isbn) {
@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.
@Path("/library")
public class Library {
@GET
@Path("/book/{isbn}")
public String getBook(@PathParam("isbn") String id) {
// search my database and get a string representation and return it
}
}
for more details : JBoss DOCS
In Spring MVC you can use the @PathVariable annotation on a method argument to bind it to the value of a URI template variable for more details : SPRING DOCS
@PathVariable and @PathParam both are used for accessing parameters from URI Template
Differences:
- As you mention
@PathVariable
is from spring and@PathParam
is from JAX-RS. @PathParam
can use with REST only, where@PathVariable
used in Spring so it works in MVC and REST.