rest

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.

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.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
venkat.
venkat.
6 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