Newsletter

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.

Note:
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
  • RestServicePathParamJava4s.java

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestPathParamAnnotationExample</groupId>
  <artifactId>RestPathParamAnnotationExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        
    </dependencies>
 
      <build>
        <finalName>RestPathParamAnnotationExample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerVersion>1.5</compilerVersion>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

 web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>RestPathParamAnnotationExample</display-name>

    <servlet>
        <servlet-name>jersey-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.java4s</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>
<!-- www.Java4s.com -->

RestServicePathParamJava4s.java

package com.java4s;
 
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;
 
@Path("/customers")
public class RestServicePathParamJava4s {
    
    @GET
    @Path("{name}/{country}")
    @Produces("text/html")
    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();
 
    }
}

Explanation

  • 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 RestServicePathParamJava4s.java 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 RestServicePathParamJava4s.java and will copy into String name, String country respectively.
  • Check the output

Output

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_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.

Comments

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

    Really an understandable explanation

  2. Sourajya says:

    Hi,
    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:

    hi,

    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.

Name*
Mail*
Website



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.