users@jersey.java.net

PathSegment and MatrixParam confusion

From: Daniel Larsson <daniel.j.larsson_at_gmail.com>
Date: Mon, 24 Nov 2008 16:18:19 +0100

Hi,
I'm having a hierarchy of resources, and want to use matrix parameters to
pass parameters to each resource in the path. I wrote a real simple example
to illustrate, pasted in below.

When using the URL
http://localhost:8080/MatrixParamTest/resources/Items/1;matrix=m1/2;matrix=m2/3;matrix=m3I
get the following logs:

// No output about PathSegment, so it's null here
getItemResource(String) 1
getItemResource(matrix) m3

Matrix param = null
getSubResource(PathSegment) 2
getSubResource(String) 2
getSubResource(matrix) m3

Matrix param = null
getSubResource(PathSegment) 3
getSubResource(String) 3
getSubResource(matrix) m3

Matrix param = null
getXml(PathSegment) 3
getXml(String) 3
getXml(matrix) m3

I had expected the PathSegment parameter to be filled in at
ItemsResource.getItemResource(), and the matrix parameter to have the value
m1 and m2 in the first two logs.

--
package com.example;
import java.util.logging.Logger;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.PathSegment;
@Path("/Items")
public class ItemsResource {
    private Logger log = Logger.getLogger(ItemsResource.class.getName());
    public ItemsResource() {}
    @Path("{id}")
    public ItemResource getItemResource(
            @PathParam("id") PathSegment ps,
            @PathParam("id") String id,
            @MatrixParam("matrix") String m) {
        if (ps != null)
            log.info("getItemResource(PathSegment) " + ps.getPath());
        log.info("getItemResource(String) " + id);
        log.info("getItemResource(matrix) " + m);
        return new ItemResource();
    }
}
--
package com.example;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.PathSegment;
public class ItemResource {
    private static Logger log =
Logger.getLogger(ItemResource.class.getName());
    @MatrixParam("matrix")
    String matrix;
    public ItemResource() {
        log.info("Matrix param = " + matrix);
    }
    @GET
    @Produces("application/xml")
    public String getXml(
            @PathParam("id") PathSegment ps,
            @PathParam("id") String id,
            @MatrixParam("matrix") String m) {
        if (ps != null)
            log.info("getXml(PathSegment) " + ps.getPath());
        log.info("getXml(String) " + id);
        log.info("getXml(matrix) " + m);
        return "Hello";
    }
    @Path("{id}")
    public ItemResource getSubResource(
            @PathParam("id") PathSegment ps,
            @PathParam("id") String id,
            @MatrixParam("matrix") String m) {
        if (ps != null)
            log.info("getSubResource(PathSegment) " + ps.getPath());
        log.info("getSubResource(String) " + id);
        log.info("getSubResource(matrix) " + m);
        return new ItemResource();
    }
}
--