users@jersey.java.net

Re: Jersey with Spring always giving 404 for subresources

From: Suneel Marthi <suneel_marthi_at_yahoo.com>
Date: Wed, 17 Nov 2010 14:19:34 -0800 (PST)

Ok, this should work.

Modify ReportsResource to read as follows:-

package com.factorlab.ws.reports;

import com.sun.jersey.api.core.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
@Path("reports")
public class ReportsResource {

    @Context protected ResourceContext resourceContext;

    @Path("testitem")
    public TestItemResource getTimelinResource() {
        return resourceContext.getResource(TestItemResource.class);;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTestText() {
        return "Success!\n";
    }
}

Its been over a year since I had worked on Jersey (or did anything RESTful),
hence the delay in response. Had to lookup old code to figure out how I had done
it.




________________________________
From: Jason Erickson <jason_at_jasonerickson.com>
To: users_at_jersey.java.net
Sent: Wed, November 17, 2010 3:10:23 PM
Subject: Re: Jersey with Spring always giving 404 for subresources

That's not it.

That is a change from when I had an actual TimelineResource bean (I still do)
but it was a much more complicated resource, so I subbed in this do-nothing one.
 Also, I'm not seeing any errors in the log about not being able to autowire the
bean, which I would if it couldn't find it, right?

One other thing - even using a Spring bean as the downstream resource was a stab
in the dark after simply creating a new TestItemResource and returning gave me
the 404. I thought maybe it had to be a Spring Bean for the SpringServlet to
figure something out, but that doesn't look like it was the problem.

In any case, I changed the name of the field to testItemResource and had no luck
- same issue. Any other ideas?


On Nov 17, 2010, at 11:44 AM, Suneel Marthi wrote:

I think the problem is with your autowiring.
>
>You have
>
>
> @Autowired
> private TestItemResource timelineResource;
>
>and by default Spring autowires 'ByName' and is expecting a bean with id =
>'timelineResource'.
>
>Try this:-
>
> @Autowired
> @Qualifier("testItemResource")
> private TestItemResource timelineResource;
>
> OR
>
> @Autowired
> private TestItemResource testItemResource;
>
> @Path("testitem")
> public TestItemResource getTimelinResource() {
> return testItemResource;
> }
>
>
>
>
>
>
________________________________
From: Jason Erickson <jerickson_at_factorlab.com>
>To: users_at_jersey.java.net
>Sent: Wed, November 17, 2010 2:24:58 PM
>Subject: Jersey with Spring always giving 404 for subresources
>
>
>I have two simple resource classes in my Spring configured web service
>application. The root one (/reports) works correctly while any path after that
>returns a 404. Here are the resource classes:
>package com.factorlab.ws.reports;
>
>import javax.ws.rs.GET;
>import javax.ws.rs.Path;
>import javax.ws.rs.Produces;
>import javax.ws.rs.core.MediaType;
>
>import org.springframework.beans.factory.annotation.Autowired;
>import org.springframework.context.annotation.Scope;
>import org.springframework.stereotype.Component;
>
>@Component
>@Scope("prototype")
>@Path("reports")
>public class ReportsResource {
>
> @Autowired
> private TestItemResource timelineResource;
>
> @Path("testitem")
> public TestItemResource getTimelinResource() {
> return timelineResource;
> }
>
> @GET
> @Produces(MediaType.TEXT_PLAIN)
> public String getTestText() {
> return "Success!\n";
> }
>}
>
>And the sub-resource is here:
>package com.factorlab.ws.reports;
>
>import javax.ws.rs.GET;
>import javax.ws.rs.Produces;
>import javax.ws.rs.core.MediaType;
>
>import org.springframework.context.annotation.Scope;
>import org.springframework.stereotype.Component;
>
>@Component
>@Scope("prototype")
>public class TestItemResource {
>
> @GET
> @Produces(MediaType.TEXT_PLAIN)
> public String hello() {
> return "Success!\n";
> }
>}
>
>I deploy the application to Jetty in a webapp called
>factorlab-ws. curl http://localhost:8080/factorlab-ws/reports yields success.
>However curl http://localhost:8080/factorlab-ws/reports/testitem gives a 404
>status.
>What could I be missing?
>
>