Deploying Jersey resources into a Servlet 3.0 API aware container fails miserably
(I've accidentally deleted the gist that I'm referring to in this question; sorry for the inconvenience.)
The Question
I'm trying to use the Servlet 3.0 API to deploy Jersey root resources (those annotated with the @Path
annotation) following the Jersey's user guide.
I've created a gist at GitHub containing two classes: Foo.java
which is a subclass of Application
that exposes Bar.java
(the resource class) through its getClasses()
method. (There is a pom.xml
too, so anyone can try this out for herself/himself easily.)
However, when I try开发者_JS百科 to deploy the packaged war to a Jetty 8.0.x instance I get the output available here at pastebin.
Foo.java
gets called, its getClasses()
method gets called too while Bar.java
is never invoked.
I can reach Jetty's welcome page at http://localhost:8080/
, however I neither can reach http://localhost:8080/foo
or http://localhost:8080/foo/bar
. The latter two results in the following error:
Not Found ERROR
custom 404 page
What could be the problem? Am I doing something wrong here?
The Answer
Given the WAR I've used (test-0.0.1-SNAPSHOT.war
) my application path became http://localhost:8080/test-0.0.1-SNAPSHOT/foo/bar
instead of http://localhost:8080/foo/bar
. See what I did there? Good. Engrave this in your mind people or lose 3-to-5 precious hours of your life!
OK. I've solved the problem.
My application's path isn't at http://localhost:8080/foo/bar
but at http://localhost:8080/<the name of my war file>foo/bar
. So given the pom.xml
I've posted it becomes http://localhost:8080/test-0.0.1-SNAPSHOT/foo/bar
.
I hate WAR files.
You've got one call with @Path, while the other have @ApplicationPath with NO @Path on the method.
As you can here,
JAX-RS API (from version 1.1.4) introduced a specific annotation ( @javax.ws.rs.ApplicationPath ), that provides an alternative to web.xml configuration:
But you'll need at least a @Path on the called method. However, the simplest thing is probably to start with a classic old web.xml, then using @Path on Resources. You'll get plenty of exemples in the web, while @ApplicationPath is not common.
While this wasn't your problem, if you're trying to deploy in Jetty 8 in Cargo, you would likely have hit this bug: http://jira.codehaus.org/browse/CARGO-1133
精彩评论