JAX-RS @MatrixParam annotation example
The idea of matrix parameters is that they are an arbitrary set of name-value pairs embedded in a uri path segment. A matrix parameter example is:
GET http://java2novice.com/spring;name=aop;[email protected]
The basic idea of matrix parameters is that it represents resources that are addressable by their attributes as well as their raw id. The @MatrixParam annotation
allows you to inject URI matrix paramters into your method invocation
This page gives you an example using JAX-RS @MatrixParam annotation, which injects value from request parameters
to your method input parameters.
In the previous examples we have given details of application setup, dependencies, web.xml file configurations: If you want to
know about these configuration, please refer these:
Restful web services using RESTEasy hello world example.
Restful web services using Jersey hello world example.
Look at the below class to apply @MatrixParam annotation:
package com.java2novice.restful;
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("/inventory")
public class InventoryService {
@GET
@Path("{deviceType}")
public Response getInventoryDetails(@PathParam("deviceType") String deviceType,
@MatrixParam("company") String company,
@MatrixParam("model") String model){
String resp = "Received request for device: "+deviceType+
", comany: "+company+" and model: "+model;
return Response.status(200).entity(resp).build();
}
}
|
In the above example, if you use "/inventory/switch;company=cisco;model=nexus-5596" URI pattern with query parameters, getInventoryDetails() method will be invoked, and you will get
below response.
Received request for device: switch, comany: cisco and model: nexus-5596
In the next page you will see an example on how to read multiple values of a query paramters
|