Jersey @PathParam Example

 

In a Jersey service, @Path is used on method level to determinate the URI path to which the method responds.

This URI path often contains variables. @PathParam is used to retrieve the value of an URI path variable.

In this example, we will see how to use @PathParam in method arguments of a JAX-RS RESTful service.

 

1. Technologies used

  • Jersey 2.13
  • Maven 3.0.5
  • Eclipse 4.4
  • JDK 1.6
  • Tomcat 6.0

 

2. Project structure

Jersey-PathParam-Example

 

3. Using @PathParam with single parameter

We will use @PathParam to parse a single URI parameter and retrieve the value of the name variable and set it into the name argument.

 

4. Using @PathParam with multiple parameters

In this case, we will use @ParamPath to mapp two URI variables into method arguments.

 

5. Using @PathParam with matrix parameter

We use PathSegment object to access to a matrix parameter, which is a set of key-value pairs embedded in the URI path.

 

6. Test It

URL : http://localhost:8080/Jersey-PathParam-Example/User/1/Walid

Jersey-PathParam-Example-1

 

URL : http://localhost:8080/Jersey-PathParam-Example/User/2/Walid/30

Jersey-PathParam-Example-2

 

URL : http://localhost:8080/Jersey-PathParam-Example/User/3/detail;name=Khadija;hobby=Manga

Jersey-PathParam-Example-3

 

Download source code