JAX-RS @PathParm Web service Example

In this tutorial we are going to see how to pass parameter to RESTful web service via URL using JAX-RS @PathParam

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey 2.x dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <name>RestTutorials Maven Webapp</name>
      <!-- JAX-RS -->
        <!-- Jersey 2.19 -->


package com.ehowtonow.webservice;

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

public class PathParamService {

 public Response getTutorial(@PathParam("index") String index) {
  return Response
    .entity("The Tutorial at Index " + index
      + " is RESTful webservice").build();

 public Response getTutorial(@PathParam("site") String site,
   @PathParam("tutorial") String tutorial, @PathParam("rank") String rank) {
  return Response
    .entity("Web Site : " + site + "  Tutorial : " + tutorial
      + " Rank : " + rank).build();


In above class @PathParam used to get the value of URL parameter that is defined in @Path expression into Java Class.

Configure Jersey 2.x Servlet dispatcher

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

	<display-name>Restful Webservice Example</display-name>


Now you can run the service and access the service by calling the following URL

http://localhost:8080/RestTutorials/rest/tutorials/1 – to access first web service api

http://localhost:8080/RestTutorials/rest/tutorials/wwww.ehowtonow.com/Java%20Tutorial/1 – to access second web service api with multiple path params

Ask your questions in eHowToNow Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. Java 8 Convert Array to Stream
  2. Java 8 New Features Overview
  3. JAX-RS @PATH for URI – Jersey
  4. Download Text file using JAX-RS RESTful Webservice
  5. How to delete directory using Java
  6. Java Tutorial – Java Environment Setup in Windows
  7. How to get the size of Collection
  8. Java Email Receiver with Attachment
  9. How to replace all occurrences of a string using RegEx
  10. First Java Application in Eclipse
  11. Java RegEx to validate Date – dd/mm/yyyy pattern
  12. Java 7 Re-throw of an Exception
  13. How to change File Last modified Date using Java
  14. Read XML File as List of Object using SAX Parser
  15. Java 8 Stream Collectors groupingBy with count and sum Example

Be the first to comment

Leave a Reply

Your email address will not be published.