第一次尝试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"; } }
带有Jersey跟踪的HTTP标头
http://localhost:8080/myservlet/dashboard:
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://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 ...
经过大量的挖掘之后,我发现问题是我的资源类位于战争内部的jar中。这碰到了 Jersey2.4.1中的一个错误,该错误已在2.5中修复。