JAX-RS REST @MatrixParam Example

We already know how to get the values using @QueryParam, @Context and get path parameter using @PathParam annotation, now lets learn how to get the value of a Matrix parameter passed using @MatrixParam Annotation. Matrix Parameter will be in the form of Key Value pairs separated by semicolon (;)

Folder Structure

  1. Create a Dynamic Web Project RestfulExample and create a package for our src files com.javainterviewpoint
  2. Place the required jar files under WEB-INF/Lib 


  3. Create the Java classes MatrixParamExample.java under  com.javainterviewpoint folder.
  4. Place the web.xml  under the WEB-INF directory

@MatrixParam Example

package com.javainterviewpoint;

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.MediaType;

public class MatrixParamExample 
    public String getMatrixParameter(
            @MatrixParam("author") String author,
            @MatrixParam("year") int year,
            @MatrixParam("book") String book)
        return "Author : "+author+" Book : "+book+" Year : "+year;

Hit on the URI :



Author : JavaInterviewPoint Book : SherlockHolmes Year : 2015

We have passed three attributes such as author, year and book as Matrix Param to the method getMatrixParameter().

Suppose we have not passed in any/one of the value then default value will be assigned to it.


Then output will be like below

Author : JavaInterviewPoint Book : null Year : 0


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee%20http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee%20http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
 <display-name>Restful Web Application</display-name>

We register our jersey container “com.sun.jersey.spi.container.servlet.ServletContainer” in the servlet-class of our web.xml and we will mention the source files location as the value to the init param “com.sun.jersey.config.property.packages” so that the container will scan for annotations of the class files within that package.

Leave a Reply

Your email address will not be published. Required fields are marked *