Jax-rs path multiple parameters example

RESTful web services:

REST stands for REpresentational State Transfer. Unlike SOAP it is a web standards based architecture and not protocol. It uses HTTP protocol for data communication. REST provides the facility to represent a resource in various formats like text, JSON and XML.

Note: JSON is the most popular format.

Jax-rs path multiple parameters jersey example

TestWS.java

package com.javawithease.ws;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType; 
 
@Path("/helloTest")
public class TestWS {	
 @GET
 @Path("{id}")
 @Produces(MediaType.TEXT_PLAIN)  
 public Response getMassegeWithSingleParam(
		                @PathParam("id") String id) {		 
	String output = "RESTful WS Jersey example. " +
			"Hello your id is : " + id; 
	return Response.status(200).entity(output).build(); 
 }
 
 @GET
 @Path("{name}/{class}/{rollNo}")
 @Produces(MediaType.TEXT_PLAIN)  
 public Response getMassegeWithMultiParam(
	@PathParam("name") String name,
	@PathParam("class") String className, 
	@PathParam("rollNo") String rollNo) {		 
	String output = "Student Name : " + name + ", Class: " 
			+className+ ", Roll No. : " + rollNo; 
	return Response.status(200).entity(output).build(); 
 }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <servlet>
  	<display-name>JAX-RS REST Servlet</display-name>
  	<servlet-name>JAX-RS REST Servlet</servlet-name>
  	<servlet-class>
  	 com.sun.jersey.spi.container.servlet.ServletContainer
  	</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>JAX-RS REST Servlet</servlet-name>
  	<url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>WS Test</title>
  </head>
 
  <body>
   <a href="services/helloTest/04">Single Parameter Test.
   </a> 
   <br/>
   <a href="services/helloTest/jai/MCA/04">Multiple Parameter Test.
   </a> 
  </body>
</html>

Output:

jax-rs example3.1
 
Click on “Single Parameter Test” link.
jax-rs example3.2
 
Click on “Multiple Parameter Test” link.
jax-rs example3.3
 
Download this example.