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 the Author .:: | ||
hi siva if my parameter contains ‘&’ sign how can i pass it?
ex:
http://localhost:8080/…/rest/customers;param1=Java&j2ee & param2=sql&plsql
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
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
@Sourajya,
I have corrected, thank you 🙂
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
Great Explanation…
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
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.
what will be annotation for extracting information for pattern /users/id=1&id=3&id=4