users@jersey.java.net

Re: Beginners questions on Uri's and templates etc.

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Mon, 10 Dec 2007 11:55:38 +0100

Hi Jeff,

would something similar to the following help?
I am not sure how exactly you handle the matrix parameters, but
as i understand it is not the main issue for you, right?

Cheers,

~Jakub

@Path("/")
public class RootResource {

    @Path("containers/{container_id}")
    public ContainerResource getContainer(@UriParam("container_id") String containerId) {
        return new ContainerResource(containerId);
    }
    
    @Path("items")
    public ItemsResource getItems() {
        return new ItemsResource(null);
    }
}

public class ContainerResource {

    String id;
    
    public ContainerResource(String id) {
        this.id = id;
    }
    
    @GET
    @ProduceMime("text/plain")
    public String getMe() {
        return "Container(id=\"" + this.id + "\")";
    }
    
    @Path("items")
    public ItemsResource getItems() {
        return new ItemsResource(this);
    }
}

public class ItemsResource {
    
    ContainerResource container;
    
    public ItemsResource(ContainerResource container) {
        this.container = container;
    }
    
    @GET
    @ProduceMime("text/plain")
    public String getMe(@MatrixParam("ids") String ids) {
        return "container: " + this.container + "items:" + ids;
    }
}

On Sat, Dec 08, 2007 at 07:42:22PM -0800, Jeff Gortatowsky wrote:
> Assume I have resources I want to expose like so:
>
> 1) items = /items/;ids=112233;ids=232144; ids=2253252
> 2) container = /container/{container_id}
> 3) items in a container = /container/{container_id)/items
>
> Number one worked great as soon as I screwed around for about 48 hours, finally looking at the test cases and finding out how to use MatrixParam().
> Number two also is a no-brainer. Works fine.
> Number three is impossible for me to figure out. I tried to forward #3 to the Items resource using a different UriTemplate than #1. When I say forward, I mean returning an newly created ItemsResource(UriInfo context) in the ContainerResource.
>
> In my mind 1 and 3 are the same 'type' of resource. Both should be handled by the items resource. They are both a list of items. One is a list of item references using a list of ids, the other by a what parent container (referenced by container_id) they are in. They both produce ItemReferencs fed into an ItemsConverter which produces the representation which is a collection/list of Item-References in XML. (Can you tell I am using CustomerDB as my basis?)
>
> Try as I might I can not get the container resource to forward to the items resource because the items resource seems to insist all items resources have to start with /items not /container - so I get a 404. What I was trying to do is when the container resource matched the "/container/{container_id)/items" resource is forward that to the items resources with the container_id and produce a list of item references. I was and am trying to be DRY. I know I can use the same method, as one takes a list of ids and the other nothing, but I can not come up with an @UriTemplate in the items resource that works. Instead it calls the first example with an empty list.
>
> When I say 'forward' I mean even though Jersey called a method in the ContainerResource, the resource returned is an ItemsResource that need further processing based in the URI to was created with.
>
> I know this sounds like a lot to bite off for a first RESTful web service, and the documentation right now is mostly in the source which is understandably as Jersey is still in development, but I was taken aback that the same resource, but in a different context, is not recognized. Maybe that is not RESTful?
>
> Thanks for reading this.
> Best wishes,
>
>
> ---------------------------------------
>
> Jeffrey D. Gortatowsky
> Fullerton, California
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
Jakub Podlesak
http://blogs.sun.com/japod