Newsletter |
RESTful Web Services (JAX-RS) @MatrixParam Example
Web Services » on Jul 8, 2014 { 4 Comments } By Sivateja
In this article i will describe how a RESTful web services would accept multiple parameters sent by the client in the HTTP URL as Matrix Params. So what are matrix parameters ? let me give you the syntax.
Matrix Parameters Syntax
Consider this URL
http://localhost:2013/<projectRoot>/rest/customers;nameKey=Java4s;countryKey=USA
If you observe the URL, i am passing 2 parameters nameKey=Java4s & countryKey=USA. One parameter is separated from another with a semicolon, similarly you can pass any number of parameters. These type of parameters are called as Matrix Parameters. I will explain more about matrix parameters in this example.
Required Files
- web.xml & pom.xml [same as previous articles]
- RestServiceMatrixParamJava4s.java
RestServiceMatrixParamJava4s.java
package com.java4s; import javax.ws.rs.GET; import javax.ws.rs.MatrixParam; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; @Path("/customers") public class RestServiceMatrixParamJava4s{ @GET @Produces("text/html") public Response getResultByPassingValue( @MatrixParam("nameKey") String name, @MatrixParam("countryKey") String country) { String output = "Customer name - "+name+", Country - "+country+""; return Response.status(200).entity(output).build(); } }
Explanation
- Once you run the application, eclipse will open the following URL http://localhost:2013/RestMatrixParamAnnotationExample/ by default
- In RestServiceMatrixParamJava4s.java [ line number 9 ] we have given class level path as /customers and we are using @MatrixParam annotation to retrieve the client inputs from the URL, so the final URL should be
http://localhost:2013/<projectRoot>/rest/customers;nameKey=Java4s;countryKey=USA
Output
You Might Also Like
::. About the Author .:: | ||
Comments
4 Responses to “RESTful Web Services (JAX-RS) @MatrixParam Example”
Nicely explaiined Web Services !!!
Nice Tutorial. But we need interview point of question.
Can you please discuss the functional difference of MatrixParam and QueryParam?
Hi,
Can you explain one example to upload files using jQuery and also retrieving for the same.