URL with matrix URI looks like:
https://www.example.com/map;lat=50;long=20;scale=32000
;lat=50;long=20;scale=32000 represents matrix parameters. Please refer W3C article on it.
Using @MatrixParam
annotation we can get matrix parameters value from API URI in Jersey Resources classes.
Using @MatrixParam at instance variable and method parameters
File : MatrixUriResource.java
package in.geekmj.resource;
import java.util.HashMap;
import java.util.Map;
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;
@Path("/matrix-uri")
@Produces(MediaType.APPLICATION_JSON)
public class MatrixUriResource {
/*
* We can inject matrix URI parameter as an instance variables
* using @MatrixParam
*/
@MatrixParam("lat")
private Integer latitude;
@MatrixParam("long")
private Integer longitude;
/* We can inject matrix URI parameter method using @MatrixParam */
@SuppressWarnings({ "unchecked", "rawtypes" })
@GET
public Map getRequestCookie(@MatrixParam("scale") Integer scale, @MatrixParam("type") String type) {
Map matrixParams = new HashMap();
matrixParams.put("latitude", latitude);
matrixParams.put("longitude", longitude);
matrixParams.put("scale", scale);
matrixParams.put("type", type);
return matrixParams;
}
}
Testing https://localhost:8080/matrix-uri;lat=50;long=20;scale=32000;type=geo with Postman chrome plugin.
API URI with matrix parameters handling in jersey
Response:
{
"latitude": 50,
"scale": 32000,
"type": "geo",
"longitude": 20
}
References
-
To know more about Matrix URIs, click here.