Newsletter

How RESTful Web Services Extract Input Parameters

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

In this article i will show you how a RESTful web service will  extract input parameters from the client request.  We have different ways of sending input values to the rest services, and RESTful web service extract those details based upon the client URL pattern. In JAX-RS we can use the following annotations to extract the input values sent by the client.

  • @PathParam
  • @QueryParam
  • @MatrixParam
  • @FormParam

@PathParam,@QueryParam,@MatrixParam are parameter annotations which allows us to map variable URI path fragments into your method call. Confused ? 🙂 In simple words, these three annotations will come into picture in case if we are passing the input values to the restful service through the URL. After that Rest service will extract those values by using these annotations. Regarding @FormParam, restful web service will use this annotation to retrieve the values sent by the client through some HTML/JSP form.

@PathParam URL Syntax

http://localhost:7001/<Rest Service Name>/rest/customers/100/Java4s

Did you observe the two parameters appear in the end of the above URL [100 & Java4s], which are separated by forward slash(/) are called as path parameters, as of now just remember the syntax, going forward i will give you an example on each annotation.

@QueryParam URL Syntax

http://localhost:7001/…/rest/customers?custNo=100&custName=Java4s

If the client sends an input in the form of query string in the URL, then those parameters are called as Query Parameters.  If you observe the above syntax, client passing 2 parameters 100 and Java4s  started after question mark (?) symbol and each parameter is separated by & symbol,  those parameters are called as query parameters.

@MatrixParam URL Syntax

http://localhost:7001/…/rest/customers;custNo=100;custName=Java4s

Matrix parameters are  another way defining the parameters to be added to URL.  If you observe the above syntax, client is passing two parameters each are separated by semicolon, these parameters are called as matrix parameters.  Remember these parameters may appear any where in the path.

@FormParam URL Syntax

Finally form parameters,  if we have a HTML form having two input fields and submit button. Lets client enter those details and submit to the RESTful web service. Then the rest service will extract those details by using this @FormParam annotation.
For now just remember these consents, going forward i will give you an example on each annotation.

​​

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 “How RESTful Web Services Extract Input Parameters”
  1. deeresh says:

    hi siva if my parameter contains ‘&’ sign how can i pass it?
    ex:
    http://localhost:8080/…/rest/customers;param1=Java&j2ee & param2=sql&plsql

  2. Sourajya says:

    Hi,

    Thanks a lot for your work. It’s helping a lot.

    I think in case of @MatrixParam, the delimiter is semicolon(;) instead of comma. I know it’s a typing mistake by you, but thought of mentioning.

    Thanks again

  3. Madhava says:

    Hi Shiva,
    can u please explain these annotations usage in real time. In which situation these wil come into pic and benefits of each and which more useful among these?

    @PathParam
    @QueryParam
    @MatrixParam
    @FormParam

  4. Siva says:

    @Sourajya,

    I have corrected, thank you 🙂

  5. Kumar says:

    As described for @PathParam URL Syntax, How can I use a parameter having slash ‘/’ (book/49) in the URL because normally it creates a different end point.

    http://localhost:7001//rest/authors/books/49

  6. Sarath says:

    Great Explanation…

  7. Suman says:

    Assume below path.

    http://localhost:7001/<Rest Service Name>/rest/customers/{customerID}/Java4s

    If I try to invoke this resource like below

    http://localhost:7001/<Rest Service Name>/rest/customers//Java4s

    what happens ???

    What if I wont pass the query param. does it throw 404 or 400 ????

    Thanks,
    Suman

  8. lokesh says:

    how to pass custom object as a parameter to webservices through url in java. Iam passing in the below pattern.
    "http://localhost:8081/webservice2/rest/details/webservice/"+cdp

    where cdp is a my pojo class.

  9. sushil kumar verma says:

    what will be annotation for extracting information for pattern /users/id=1&id=3&id=4

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.