JAX-RS @MatrixParam example

This post is in continuation with web service tutorial (Part -10).
    Introduction to web services Web services interview questions SOAP web service introduction RESTful web service introduction Difference between SOAP and REST web services SOAP web service example in java using eclipse JAX-WS web service eclipse tutorial JAX-WS web service deployment on tomcat Create RESTful web service in java(JAX-RS) using jersey RESTful web service JAXRS json example using jersey RESTful web service JAXRS CRUD example using jersey AngularJS RESTful web service JAXRS CRUD example using $http RESTful Web Services (JAX-RS) @QueryParam Example Spring Restful web services simple example Spring Restful web services json example Spring Restful web services CRUD example
In previous post, we have used @QueryParam for passing parameter to URL in JAX-RS @QueryParam example and  In case of @QueryParam, we pass parameters as well as its values separated by ? but in case of @MatrixParam, key value pairs are separared by semicolon(;). 

For example:
http://localhost:8080/JAXRSJsonExample/rest/countries/country;id=1;countryName=India
Here Parameter names are id,countryName and its values are 3,Nepal respectively separated by ;.

Here are steps to create a JAXRS json example which will use @MatrixParam.

1) Follow steps on RESTful Web Services json example to create simple RESTful web services which uses @PathParam.
2) Change CountryRestService.java to use @MatrixParam as below.

package org.arpit.java2blog.jaxrs;

import java.util.ArrayList;
import java.util.List;

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;

import org.arpit.java2blog.bean.Country;
/* 
 * author: Arpit Mandliya
 * */
@Path("/countries")
public class CountryRestService {
 
    @GET
    @Produces(MediaType.APPLICATION_JSON)
 public List<Country> getCountries()
 {
  List<Country> listOfCountries = new ArrayList<Country>();
  listOfCountries=createCountryList();
  return listOfCountries;
 }

 @GET
    @Path("/country")
    @Produces(MediaType.APPLICATION_JSON)
 public Country getCountryById(@MatrixParam("id") int id,@MatrixParam("countryName") String countryName)
 {
  List<Country> listOfCountries = new ArrayList<Country>();
  listOfCountries=createCountryList();

  for (Country country: listOfCountries) {
   if(country.getId()==id && country.getCountryName().equalsIgnoreCase(countryName))
    return country;
  }
  
  return null;
 }

// Utiliy method to create country list.
 public List<Country> createCountryList()
 {
  Country indiaCountry=new Country(1, "India");
  Country chinaCountry=new Country(4, "China");
  Country nepalCountry=new Country(3, "Nepal");
  Country bhutanCountry=new Country(2, "Bhutan");

  List<Country> listOfCountries = new ArrayList<Country>();
  listOfCountries.add(indiaCountry);
  listOfCountries.add(chinaCountry);
  listOfCountries.add(nepalCountry);
  listOfCountries.add(bhutanCountry);
  return listOfCountries;
 }
}

That's all. We need to run the application now.

Run the application

3) Right click on project -> run as -> run on server
Select apache tomcat and click on finish



4) Now pass country id and country name as parameters to url separated by ;.
"http://localhost:8080/JAXRSJsonExample/rest/countries/country;id=1;countryName=India".


We are done with Restful web services json MatrixParam example. If you are still facing any issue, please comment.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012