73

To my knowledge both serves the same purpose. Except the fact that @PathVariable is from Spring MVC and @PathParam is from JAX-RS. Any insights on this?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
suresh
  • 943
  • 2
  • 8
  • 22
  • 3
    possible duplicate of [@RequestParam vs @PathVariable](http://stackoverflow.com/questions/13715811/requestparam-vs-pathvariable) – Bond - Java Bond Sep 03 '15 at 05:37
  • 1
    Suresh, i like your own answer better than the answers below. I think perhaps it's important to note that 1) Spring honors PathParam as well as its own PathVariable, and 2) whenever possible Java's PathParam is preferrable (because you may replace Spring some day with another technology, but as long as you are working with Java...) – inor Jan 15 '18 at 09:00
  • Expecting answers regarding cases on when to to use path variable and when to use path parameter. – Devanshu Kashyap Nov 29 '19 at 13:10

7 Answers7

73

@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.

See also: Difference between @RequestParam and @QueryParam Anotation

Premraj
  • 72,055
  • 26
  • 237
  • 180
45

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) {
Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50
11

@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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
FuSsA
  • 4,223
  • 7
  • 39
  • 60
  • 3
    in other words, it does the same, but `@PathVariable` is the equivalent to be used in Spring (?) – phil294 Mar 15 '17 at 20:40
1

@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)

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
0

Some can use @PathParam in Spring as well but value will be null when URL request is being made Same time if We use @PathVarriable then if value is not being passed then application will throw error

-2

@PathVariable

@PathVariable it is the annotation, that is used in the URI for the incoming request.

http://localhost:8080/restcalls/101?id=10&name=xyz

@RequestParam

@RequestParam annotation used for accessing the query parameter values from the request.

public String getRestCalls(
@RequestParam(value="id", required=true) int id,
@RequestParam(value="name", required=true) String name){...}

Note

whatever we are requesting with rest call i.e, @PathVariable

whatever we are accessing for writing queries i.e, @RequestParam

suresh
  • 943
  • 2
  • 8
  • 22
Mohammad Adil
  • 503
  • 6
  • 13
  • 1
    though my question is difference between pathvariable and pathparam. Thank you for the reply . – suresh Nov 01 '17 at 02:23
-2

@PathParam: it is used to inject the value of named URI path parameters that were defined in @Path expression.

Ex:

@GET
@Path("/{make}/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make, @PathParam("model") PathSegment car, @PathParam("year") String year) {
        String carColor = car.getMatrixParameters().getFirst("color");

}

@Pathvariable: This annotation is used to handle template variables in the request URI mapping ,and used them as method parameters.

Ex:

     @GetMapping("/{id}")
     public ResponseEntity<Patient> getByIdPatient(@PathVariable Integer id) {
          Patient obj =  service.getById(id);
          return new ResponseEntity<Patient>(obj,HttpStatus.OK);
     }