Skip to content
Go back

Jersey (JAX-RS) matrix URI parameters handling guide

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.

Response:

{
  "latitude": 50,
  "scale": 32000,
  "type": "geo",
  "longitude": 20
}

References

  1. To know more about Matrix URIs, click here.

  2. Official Jersey Documentation

  3. Download the Full Project

  4. Follow Project On Github


Share this post on:

Previous Post
Jersey (JAX-RS) Java types to consume request parameter values guide
Next Post
Working with request cookie in Jersey (JAX-RS) guide