Get all updates of Java4s
Follow Us: Email Newsletter

How RESTful Web Services Extract Input Parameters

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

7 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

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.