RESTful Web Services (JAX-RS) @PathParam Example

Web Services » on Jul 8, 2014 { 9 Comments } By Sivateja

In RESTful (JAX-RS) web services @PathParam annotation will be used to bind RESTful URL parameter values to the method arguments. Lets discuss with a simple example.

If you are new to RESTful web services or if you would like to know complete step by step flow of JAX-RS, Go through this article ‘Jersey Hello World example With Maven in Eclipse Juno‘, then only you will be able to understand this tutorial 🙂 and even further web services tutorials.

Required Files

  • pom.xml
  • web.xml


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
            <name> Repository for Maven</name>




<web-app id="WebApp_ID" version="2.4"
    xmlns="" xmlns:xsi=""



<!-- -->

package com.java4s;
public class RestServicePathParamJava4s {
    public Response getResultByPassingValue(
                    @PathParam("name") String name,
                    @PathParam("country") String country) {
        String output = "Customer name - "+name+", Country - "+country+"";
        return Response.status(200).entity(output).build();


  • Right click on your project > Run As > Run on Server
  • By default eclipse will open http://localhost:2013/RestPathParamAnnotationExample/ with  HTTP 404 Error
  • In web.xml we have specified URL pattern as /rest/* (line number 19) and in we specified class level @path as /customers [ line number 9 ] and method level @path as  {name}/{country} [ line number 13 ]
  • So the final URL should be http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4s/USA
  • Once you hit the URL,  http://localhost:2013/……/rest/../Java4s/USA  , last two parameters in this URL ‘Java4s‘ and ‘USA‘ are retrieved by @PathParam(“name”), @PathParam(“country”) annotations in and will copy into String name, String country respectively.
  • Check the output



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


9 Responses to “RESTful Web Services (JAX-RS) @PathParam Example”
  1. Krishna says:

    Really an understandable explanation

  2. Sourajya says:

    Can you please explain me the use of init-param here?

    The values you have provided under param-name and param-value tags, I can’t understand.

    • prabinakumarpanda says:

      init-param means we are registering our resource into implementation vendor ,if you not provide your init-param it will scan the package.

  3. Aakash Ahmed says:

    Sivateja Kandula Your Really Done WonderFull Job And Keep It Up..!

  4. vijay says:


    this is vijay, please help me . i want to send JSON data as request to REST web service and want to store JSON element to data base and response to client as successful read json elements.

  5. mohit says:

    hello, this is mohit .please help me i want to create request in xml format and then that xml send as request with url, and want to get back response in xml format

  6. Hello sir,

    Nice and superb explanation !!!!!!!!!!!

  7. Anu says:

    How to find factorial of a number by using restful web services

  8. Kalaivany says:

    Thank you so much for the explanation.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.