users@jersey.java.net

RE: [Jersey] Design Question / Sub Resources

From: Rabick, Mark A (IS) <"Rabick,>
Date: Thu, 21 May 2009 15:29:11 -0500

> 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.

Got it and it works fine...

        public NodeV1Resource getNodeV1Resource(
                                                                @Context
ResourceContext rc,
        
@PathParam("nodesk") String nodesk) {

                if (nodesk == null)
                        throw new BadRequestException("SK paramater
required.");

                NodeV1Resource nr =
rc.getResource(NodeV1Resource.class);
**** nr.setNodeSk(nodesk);
                
                return nr;
        }

The previous version set 2 params in the constructor of the sub
resource, one of which was an @EJB injected element. Some of my
subresources require 4 or 5 of those remote instances so the tradeoff
with a single 'setter' is fine.

Thanks again,

-mark

_______________________________________________
Mark A. Rabick - Software Engineer
Em: mark.rabick_at_ngc.com

 

> -----Original Message-----
> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> Sent: Thursday, May 21, 2009 3:02 PM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Design Question / Sub Resources
>
>
> 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.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>