开发者

Jersey with Spring always giving 404 for subresources

开发者 https://www.devze.com 2023-01-25 20:18 出处:网络
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:

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.

Also, I put breakpoints in each of the methods in ReportsResouce. getTestText() breaks fine, but getTimelineResource() doesn't, implying that it never enters that method.

What could I be m开发者_JAVA百科issing?


I figured out the problem - it was in my web.xml. I had configured several paths for servlet mapping to the Jersey Spring servlet, but that didn't work. What did work was:

<servlet-mapping>
    <servlet-name>jersey-spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I could not get any other mapping to work - giving me 404's on everything except for the explicit url-pattern. So, this solves my problem, but does anyone know if this is a bug? Or is there some reason why this is supposed to be the way it works?

0

精彩评论

暂无评论...
验证码 换一张
取 消