users@jersey.java.net

Re: [Jersey] Resource sub-locators not working as expected.

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 21 Apr 2009 15:18:03 -0700

Remove the @GET on getModelResource. Sub-resource locators don't have
a method designator.

Marc.

On Apr 21, 2009, at 2:58 PM, Jalpesh Patadia wrote:

> Hello everyone,
>
> Thanks to Paul for responding to my earlier issue. I have resolved
> that, and am facing another issue; which I think may be a bug in the
> implementation. Here’s some dummy code which explains this:
>
> Resource 1 : Car Resource
>
> @Path("/1.0/cars")
> public class CarResource extends AbstractResource
> {
> @GET
> @Path("list") // lists all the cars -- general pattern for
> listing a resource
> @Produces({"application/xml","application/json","text/
> csv"}) // optional, but putting it will help documentation
> public List<Car> getFormattedCarList(@HeaderParam("Accept")
> String accept, @Context HttpServletRequest req)
> {
> System.out.println("Accept : " + accept);
> System.out.println("Request: " + req);
> return super.getCarList();
> }
> …
> @Path("model")
> @GET
> public CarModelResource getModelResource()
> {
> return new CarModelResource();
> }
> }
>
>
> Resource 2 : CarModelResource
>
> // @Path("/1.0/cars/model")
> public class CarModelResource extends AbstractResource
> {
> @Documentation(description="Get a list of car models detail list")
> @GET
> @Path("list")
> public List<CarModel> getCarModelListFormatted()
> {
> System.out.println("+++++ Returning formatted list +++++");
> return super.getCarModelList();
> }
> }
>
> Now, according to the spec, the CarModelResource is a sub-resource
> with a sub-resource locator getCarModelListFormatted. What I’m
> seeing is that when I type in http://myurl.../1.0/cars/model/list
> this code is returning me a 404 – Not found error. Maybe I’m not
> reading the spec correctly, but upon reading the JAX-RS Spec,
> section 3.4.1 – Sub Resources it implied that this should work.
> Could somebody shed some light why it’s not working ?
>
> Now, interestingly I got this working by uncommenting the @Path
> annotation in CarModelResource, so it’s not a problem. But at that
> point, I don’t think it’s actually a sub-resource locator anymore
> but just a regular resource mapping. In the example described in the
> spec the WidgetResource class does not have a @Path annotation on
> it, so I assumed that it’s not needed.
>
> I just needed to understand what is the correct behavior.
>
>
> Thanks,
>
> Jalpesh.
>
> PS : I’m using the com.sun.jersey…..SpringServlet in web.xml as my
> servlet handler. I have verified that all the beans are registered
> correctly in the applicationContext files.
>
>
> PRIVILEGED AND CONFIDENTIAL
> This transmission may contain privileged, proprietary or
> confidential information. If you are not the intended recipient, you
> are instructed not to review this transmission. If you are not the
> intended recipient, please notify the sender that you received this
> message and delete this transmission from your system.