Get all updates of Java4s
Follow Us: Email Newsletter

RESTful Web Services (JAX-RS) @PathParam Example

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

 web.xml

RestServicePathParamJava4s.java

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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

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

  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. Raj Sidduri says:

    Hello sir,

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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