Friday, April 27, 2012

@MatrixParam Implementation in JAX-RS

In this tutorial , i will show usage of @MatrixParam. Matrix parameters are a set of “name=value” in URI path, for example, /books/2012;author=anuj
In above URI, the matrix parameter is “author=anuj“, separate by a semi colon “;“.


@MatrixParam example
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/books")
public class BookService {

 @GET
 @Path("{year}")
 public Response getBooks(@PathParam("year") String year,
   @MatrixParam("author") String author,
   @MatrixParam("country") String country) {

  return Response
   .status(200)
   .entity("getBooks is called, year : " + year
    + ", author : " + author + ", country : " + country)
   .build();

 }
}



Results :
1. URI Pattern : “/books/2012/”
getBooks is called, year : 2012, author : null, country : null
2. URI Pattern : “/books/2012;author=anuj”
getBooks is called, year : 2012, author : anuj, country : null
3. URI Pattern : “/books/2012;author=anuj;country=usa”
getBooks is called, year : 2012, author : anuj, country : usa
4. URI Pattern : “/books/2012;country=usa;author=anuj”
getBooks is called, year : 2012, author : anuj, country : usa

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/