Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. 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.

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 : JAXRS-HelloWorld.zip

1. Project structure

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

project-structure

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

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

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Restful Web Application</display-name>

	<servlet>
		<servlet-name>jersey-helloworld-serlvet</servlet-name>
		<servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
		<init-param>
		     <param-name>com.sun.jersey.config.property.packages</param-name>
		     <param-value>com.javacodegeeks.enterprise.rest.jersey</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-helloworld-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

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 :

http://localhost:8080/JAXRS-HelloWorld/rest/....

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

2. Single parameter

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

HelloWorldREST.java:

package com.javacodegeeks.enterprise.rest.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/members")
public class HelloWorldREST {

	@GET
	@Path("{username}")
	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:

http://localhost:8080/JAXRS-HelloWorld/rest/members/nikos

Outputs:

This is all the info on member: nikos

And

http://localhost:8080/JAXRS-HelloWorld/rest/members/james

Outputs:

This is all the info on member: james

3. Multiple Parameters

This is how you can parse multiple Path Parameters.

HelloWorldREST.java:

package com.javacodegeeks.enterprise.rest.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/members")
public class HelloWorldREST {

	@GET
	@Path("{username}/{classId}/{semester}")
	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:

http://localhost:8080/JAXRS-HelloWorld/rest/members/james/syssec/spring

Outputs:

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.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close