@MatrixParam with example
What is @MatrixParam ?
It is the annotation defined in JAX-RS specification which is used to resolve the values passed in the URL with the Key-Value pair.
Each key value pair will be separated by semicolon(;)
This annotation will be used at the method argument level.
We should use the key with @MatrixParam annotation to resolve the actual value associated for that key.
Example:
http://sample.com/user;name=john
In this example, ‘name’ is passed as a key value pair.
For the above example, we can use @MatrixParam in method argument as below
(@MatrixParam(“name”) String name)
http://sample.com/user;name=john;age=45
In this example, “name” and “age” are passed as key value pairs.
For the above example, we can use @MatrixParam in method argument as below
(@MatrixParam(“name”) String name, @MatrixParam(“age”) String age)
Let’s see the complete example on this
Create a new Maven Web project in eclipse (Refer Rest service Hello World project for the same)
Project structure
Step 1
Update pom.xml with below dependencies
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>MatrixParam</groupId>
- <artifactId>MatrixParam</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>MatrixParam Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.glassfish.jersey.containers</groupId>
- <artifactId>jersey-container-servlet</artifactId>
- <version>2.24</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>MatrixParam</finalName>
- </build>
- </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>MatrixParam</groupId> <artifactId>MatrixParam</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>MatrixParam Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.24</version> </dependency> </dependencies> <build> <finalName>MatrixParam</finalName> </build> </project>
We have added dependencies for Jersey servlet and Junit in the above pom file.
Step 2
Update web.xml file with Jersey servlet container
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
- <web-app>
- <display-name>Archetype Created Web Application</display-name>
- <servlet>
- <servlet-name>jersey-serlvet</servlet-name>
- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
- <init-param>
- <param-name>jersey.config.server.provider.packages</param-name>
- <param-value>com.kb.rest</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>jersey-serlvet</servlet-name>
- <url-pattern>/rest/*</url-pattern>
- </servlet-mapping>
- </web-app>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.kb.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
we have defined a special servlet called “jersey-serlvet” in web.xml and mapped it by the URL pattern /rest/*
Any request matching the above pattern will be handled by jersey Servlet.
Step 3
Create MatrixParamUserService as below
- package com.kb.rest;
- 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("/user")
- public class MatrixParamUserService {
- @GET
- @Produces("text/html")
- public Response getUserByName(@MatrixParam("name") String name){
- String output =null;
- //Service call to Get user Data based on id from DB
- if(null !=name){
- output ="user details for Name: "+name+ ", state : New jersey,Country:USA,age:45";
- }
- else{
- output="Sorry user not found !";
- }
- return Response.status(200).entity(output).build();
- }
- @Path("/details")
- @GET
- @Produces("text/html")
- public Response getUserByNameAndAge(@MatrixParam("name") String name,@MatrixParam("age") String age){
- //Service call to Get user Data based on name and state from DB
- String output ="user details : Name:John, state : New jersey,Country:USA,age:45";
- return Response.status(200).entity(output).build();
- }
- }
package com.kb.rest; 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("/user") public class MatrixParamUserService { @GET @Produces("text/html") public Response getUserByName(@MatrixParam("name") String name){ String output =null; //Service call to Get user Data based on id from DB if(null !=name){ output ="user details for Name: "+name+ ", state : New jersey,Country:USA,age:45"; } else{ output="Sorry user not found !"; } return Response.status(200).entity(output).build(); } @Path("/details") @GET @Produces("text/html") public Response getUserByNameAndAge(@MatrixParam("name") String name,@MatrixParam("age") String age){ //Service call to Get user Data based on name and state from DB String output ="user details : Name:John, state : New jersey,Country:USA,age:45"; return Response.status(200).entity(output).build(); } }
In this service, we have written 2 methods, one will get the user details based on “name” and other will get the user details based on “name” and “age”
In the first method,
We are passing “name” as a key value pair to get the user details.
In the second method,
We are passing name and age as a key value pairs to get the user details
Step 4
Build and deploy the project
Step 5
Let’s see the output
Access below URL
http://localhost:8080/MatrixParam/rest/user
In the above URL request, we are not passing any matrix parameters and hence they will be null in the method and hence appropriate error message is shown.
Access below URL
http://localhost:8080/MatrixParam/rest/user;name=john
Access below URL
http://localhost:8080/MatrixParam/rest/user/details;name=john;age=45