users@jersey.java.net

Re: [Jersey] Design Question / Sub Resources

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 21 May 2009 22:02:18 +0200

On May 21, 2009, at 9:43 PM, Rabick, Mark A (IS) wrote:

> Paul/Anyone,
>
> You mentioned earlier in this thread the use of @ResourceContext to
> get
> managed sub-resources:
>
>>> It is possible to inject information into sub-resources using
>>> the ResourceContext class.
>>>
>>> @Path("{id}/branch")
>>> public BranchResource getBranchResource(@Context ResourceContext
>>> rc) {
>>> ScopedBranchResource sbr =
>>> rc.getReource(ScopedBranchResource.class);
>>> ...
>>> return sbr;
>>> }
>
> Is the above code correct? Shouldn't the line in the body be:
>
> ScopedBranchResource sbr = rc.getReource(BranchResource.class);
>

Only if BranchResource extends ScopedBranchResource i.e. an instance
of the class declared in the parameter is returned.


> My other question is that the @Path has a param {id}. In my case, I'm
> passing the bound id value to the constructor of my subresource. Is
> this possible?
>
> @Path("{key}")
> public NodeResource getNodeResource(@PathParam("key") String
> key) {
>
> return new NodeV1Resource(key, nodeRemote);
> }
>
> The nodeRemote variable is a local variable in the parent resource
> class
> that is a remote interface to an EJB that I'm presently passing to the
> subresource because it is need to process operations on Node
> instances.
>
> How would the above method be constructed using the @Context
> ResourceContext rx?
>


You could need to call a setter method instance. I realize this is not
ideal, but unfortunately I cannot come up with a good way to pass in
such parameters in a type safe manner for construction.

Paul.