未找到泽西岛资源

时间:2013-12-19 16:20:34

标签: java tomcat jersey

第一次尝试泽西岛时,我很快遇到了一个我无法弄清楚的问题。我在同一个包中有两个资源,遵循相同的蓝图,但在运行时,Jersey只找到其中​​一个。我已经检查过两个类都存在于战争中包含的jar中的同一个文件夹中。

更新:如果我删除DashboardResource上的@Path注释(有效删除资源),那么DetailsResource突然变为现实。仪表板似乎以某种方式隐藏了细节。

迫切需要一些如何解决这个问题的想法!

事实和代码

版本:Jersey 2.4.1,Apache Tomcat 7.0.47

这个很好用:

package jerseytest;

import javax.ws.rs.*;

@Path("/dashboard")
public class DashboardResource {
  @GET
  @Produces("text/plain")
  public String getDashboard() { return "Dashboard"; }
}

这个给了我一个HTTP 404:

package jerseytest;

import javax.ws.rs.*;

@Path("/details")
public class DetailsResource {
  @GET
  @Produces("text/plain")
  public String getDetails() { return "Details"; }
}

有泽西追踪的HTTP标头

http://localhost:8080/myservlet/dashboard

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Jersey-Tracing-000: START       [ ---- /  ---- ms |  ---- %] baseUri=[http://localhost:8080/myservlet/] requestUri=[http://localhost:8080/myservlet/dashboard] method=[GET] authScheme=[n/a] accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8] accept-encoding=[gzip,deflate,sdch] accept-charset=n/a accept-language=[en-US,en;q=0.8,da;q=0.6,nb;q=0.4,sv;q=0.2] content-type=n/a content-length=n/a
X-Jersey-Tracing-001: PRE-MATCH   [ 0,01 /  2,45 ms |  0,10 %] PreMatchRequest summary: 0 filters
X-Jersey-Tracing-002: MATCH       [ ---- /  2,90 ms |  ---- %] Matching path [/dashboard]
X-Jersey-Tracing-003: MATCH       [ ---- /  3,02 ms |  ---- %] Pattern [/dashboard(/)?] IS selected
X-Jersey-Tracing-004: MATCH       [ ---- /  3,38 ms |  ---- %] Matched resource: template=[/dashboard] regexp=[/dashboard(/.*)?] matches=[/dashboard] from=[/dashboard]
X-Jersey-Tracing-005: MATCH       [ ---- /  4,40 ms |  ---- %] Matched method  : public java.lang.String jerseytest.DashboardResource.getDashboard()
X-Jersey-Tracing-006: MATCH       [ ---- /  4,95 ms |  ---- %] Resource instance: [jerseytest.DashboardResource @5814b2a3]
X-Jersey-Tracing-007: MATCH       [ 2,63 /  5,17 ms | 30,68 %] RequestMatching summary
X-Jersey-Tracing-008: REQ-FILTER  [ 0,01 /  5,32 ms |  0,09 %] Request summary: 0 filters
X-Jersey-Tracing-009: INVOKE      [ 0,02 /  6,13 ms |  0,23 %] Resource [jerseytest.DashboardResource @5814b2a3] method=[public java.lang.String jerseytest.DashboardResource.getDashboard()]
X-Jersey-Tracing-010: INVOKE      [ ---- /  6,31 ms |  ---- %] Response: [org.glassfish.jersey.message.internal.OutboundJaxrsResponse @36dc1882 <200/SUCCESSFUL|OK|java.lang.String @66702fb1>]
X-Jersey-Tracing-011: RESP-FILTER [ 0,01 /  6,81 ms |  0,09 %] Response summary: 0 filters
X-Jersey-Tracing-012: WI          [ 0,01 /  7,12 ms |  0,08 %] [org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor @5e62c4bb #10] BEFORE context.proceed()
X-Jersey-Tracing-013: WI          [ 0,01 /  7,23 ms |  0,17 %] [org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor @26620166 #3000] BEFORE context.proceed()
X-Jersey-Tracing-014: MBW         [ ---- /  7,36 ms |  ---- %] Find MBW for type=[java.lang.String] genericType=[java.lang.String] mediaType=[[javax.ws.rs.core.MediaType @6c49e339]] annotations=[@javax.ws.rs.GET(), @javax.ws.rs.Produces(value=[text/plain])]
X-Jersey-Tracing-015: MBW         [ ---- /  7,60 ms |  ---- %] [org.glassfish.jersey.message.internal.StringMessageProvider @74e48049] IS writeable
X-Jersey-Tracing-016: MBW         [ 0,14 /  7,84 ms |  1,61 %] WriteTo by [org.glassfish.jersey.message.internal.StringMessageProvider @74e48049]
X-Jersey-Tracing-017: WI          [ 0,00 /  7,94 ms |  0,04 %] [org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor @26620166 #3000] AFTER context.proceed()
X-Jersey-Tracing-018: WI          [ 0,01 /  8,39 ms |  0,08 %] [org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor @5e62c4bb #10] AFTER context.proceed()
X-Jersey-Tracing-019: WI          [ 1,38 /  8,48 ms | 16,11 %] WriteTo summary: 2 interceptors
X-Jersey-Tracing-020: FINISHED    [ ---- /  8,56 ms |  ---- %] Response status: 200/SUCCESSFUL|OK
Content-Type: text/plain
Content-Length: 10
Date: Thu, 19 Dec 2013 16:18:03 GMT

http://localhost:8080/myservlet/details

HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
X-Jersey-Tracing-000: START       [ ---- /  ---- ms |  ---- %] baseUri=[http://localhost:8080/myservlet/] requestUri=[http://localhost:8080/myservlet/details] method=[GET] authScheme=[n/a] accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8] accept-encoding=[gzip,deflate,sdch] accept-charset=n/a accept-language=[en-US,en;q=0.8,da;q=0.6,nb;q=0.4,sv;q=0.2] content-type=n/a content-length=n/a
X-Jersey-Tracing-001: PRE-MATCH   [ 0,01 /  3,74 ms |  0,20 %] PreMatchRequest summary: 0 filters
X-Jersey-Tracing-002: MATCH       [ ---- /  4,25 ms |  ---- %] Matching path [/details]
X-Jersey-Tracing-003: MATCH       [ 0,54 /  4,39 ms | 10,19 %] RequestMatching summary
X-Jersey-Tracing-004: RESP-FILTER [ 0,01 /  5,18 ms |  0,13 %] Response summary: 0 filters
X-Jersey-Tracing-005: FINISHED    [ ---- /  5,28 ms |  ---- %] Response status: 404/CLIENT_ERROR|Not Found
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 967
Date: Thu, 19 Dec 2013 16:18:47 GMT

的web.xml:

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">

    <display-name>My Web App</display-name>

    <servlet>
        <display-name>My Servlet</display-name>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>jerseytest</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.tracing</param-name>
            <param-value>ALL</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

泽西岛部署日志:

...
[2013-12-20 10:38:03,986] Artifact jerseytest-war:war: Artifact is being deployed, please wait...
Connected to server
des 20, 2013 10:38:06 AM org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer addServletWithDefaultConfiguration
INFO: Registering the Jersey servlet application, named javax.ws.rs.core.Application, with the following root resource and provider classes: [class jerseytest.DashboardResource, class org.glassfish.jersey.server.wadl.internal.WadlResource, class jerseytest.DetailsResource, class org.glassfish.jersey.client.filter.HttpDigestAuthFilter]
des 20, 2013 10:38:06 AM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4.1 2013-11-08 12:08:47...
[2013-12-20 10:38:07,184] Artifact jerseytest-war:war: Artifact is deployed successfully
...

2 个答案:

答案 0 :(得分:0)

似乎DetailsResource容器找不到Jersey,打开war并在classeslib \ jars下查看DetailsResource 1}}实际上已部署。

<强>更新

Jersey配置中,发生的事情是只有一个资源被注册(我假设为root资源)。这也解释了在删除details之后dashboard工作的原因。

我认为您可以尝试添加第一个root-resource,其中@Path("/")@Path("v1")(其中v1对应于版本号,例如)。现在,此根资源将链接所有可用资源:

@Component
@Path("/")
public class BaseResource
{
    @Autowired //if using spring and interfaces for resource, else set it via setter/constructor
    private DashboardResource dashboardResource;

    @Autowired //if using spring and interfaces for resource, else set it via setter/constructor
    private DetailsResource detailsResource;

    @Path("dashboard")
    public DashboardResource getDashboardResource()
    {
        return dashboardResource;
    }

    @Path("details")
    public DetailsResource getDetailsResource()
    {
        return detailsResource;
    }
}

请记住从详细信息和仪表板资源类中删除@Path注释。

答案 1 :(得分:0)

经过大量的挖掘后,我发现问题在于我在战争中的jar中有我的资源类。这在泽西岛2.4.1中命中a bug,修正为2.5。