JAX-RS @PathParam Example

In this example we are going to talk about how you can use @PathParam annotation to parse Path Parameters in a JAX-RS RESTful service. Basically, @PathParam denotes that the value of the Path Parameter with the corresponding name, declared previously in the @Path annotation above the targeted method, will be parsed and if parsed correctly it will be available on the method argument denoted with @PathParam.

In this example we are not going to focus on how to create JAX-RS application from top to bottom. So make sure you read carefully Jersey Hello World Example and RESTEasy Hello World Example, and pay attention to the sections concerning the creation of the project with Eclipse IDE as well as the deployment of the project in Tomcat.

The code of this tutorial is going to be based on Jersey Hello World Example. You can download the Eclipse project of this tutorial here :

1. Project structure

Let’s remind ourselves the structure of the project we are working on:


The code presented in this new tutorial will only concern file.

At this point you can also take a look at the web.xml file to see how the project is configured:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
  <display-name>Restful Web Application</display-name>



As you can see our servlet is mapped to /rest/ URI pattern. So the basic structure of the URIs used in this example will have the form :


So let’s see how to use @PathParam annotation.

2. Single parameter

Let’s see how we can parse a single Path Parameter :



public class HelloWorldREST {

	public Response geMemberInfo( @PathParam("username") String username ) {

		String output = " This is all the info on member: " + username;

		// . . . work to get member's info

		return Response.status(200).entity(output).build();

Using @Path("{username}") we defined a new path parameter with name username. We then use @PathParam("username") to parse the value and push it to String username argument. Not that the argument variable can take any name you want but it’s usually convenient to name it after the corresponding parameter.

When you put on your browser:



This is all the info on member: nikos




This is all the info on member: james

3. Multiple Parameters

This is how you can parse multiple Path Parameters.



public class HelloWorldREST {

	public Response getMemberGrade( @PathParam("username") String username,  @PathParam("classId") String classId,  @PathParam("semester") String semester  ) {

		String output = " This is the grade of " + username +" in class "+ classId +" of "+semester +" semester :";

		// . . . work to get memeber's grade

		return Response.status(200).entity(output).build();

When you put on your browser:



This is the grade of james in class syssec of spring semester :

It’s important to note that @PathParam can only be used on the following Java types:

  • All primitive types except char
  • All wrapper classes of primitive types except Character
  • Have a constructor that accepts a single String argument
  • Any class with the static method named valueOf(String) that accepts a single String argument
  • Any class with a constructor that takes a single String as a parameter
  • List<T>, Set<T>, or SortedSet<T>, where T matches the already listed criteria. Sometimes parameters may contain more than one value for the same name. If this is the case, these types may be used to obtain all values.

This was an example on the usage of JAX-RS @PathParam annotation.

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
5 years ago

Actually….we are using Apache camel….when i m trying to use multiple parameters…only first value is printed and rest other values were given null vaule

Back to top button