Newsletter

RESTful Web Services (JAX-RS) @MatrixParam Example

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

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

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

4 Responses to “RESTful Web Services (JAX-RS) @MatrixParam Example”
  1. vishwnath says:

    Nicely explaiined Web Services !!!

  2. PugaZhendhi says:

    Nice Tutorial. But we need interview point of question.

  3. Rabindra says:

    Can you please discuss the functional difference of MatrixParam and QueryParam?

  4. Prakhar says:

    Hi,

    Can you explain one example to upload files using jQuery and also retrieving for the same.

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 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.