(我不小心删除了我在这个问题中提到的要点;很抱歉给您带来不便。)
问题
我尝试使用的 Servlet 3.0用于部署 Jersey 根资源的 API(那些用 @Path
注释)遵循 Jersey 的用户指南。
我在 GitHub 上创建了一个 gist ,其中包含两个类:Foo.java
它是 应用程序
公开Bar.java
(资源类)通过它的getClasses()
方法。 (有一个 pom.xml
太,所以任何人都可以轻松地自己尝试一下。)
但是,当我尝试部署将 war 打包到 Jetty 8.0.x 实例,我得到 pastebin 处可用的输出。
Foo.java
被调用,它的 getClasses()
方法也被调用,而 Bar.java
从未被调用。
我可以通过 http://localhost:8080/
访问 Jetty 的欢迎页面,但是我都无法访问 http://localhost:8080/foo
或 http: //本地主机:8080/foo/bar
。后两者会导致以下错误:
未找到错误
自定义 404 页面
可能是什么问题?我在这里做错了什么吗?
答案
鉴于我使用过的 WAR (test-0.0.1-SNAPSHOT.war
),我的应用程序路径变成了 http://localhost :8080/test-0.0.1-SNAPSHOT/foo/bar
而不是 http://localhost:8080/foo/bar
。看看我在那里做了什么?好的。各位,请将此铭记在心,否则就会失去生命中 3 到 5 个小时的宝贵时间!
(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 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!
发布评论
评论(3)
好的。我已经解决了这个问题。
我的应用程序的路径不在
http://localhost:8080/foo/bar
而是在http://localhost:8080/<我的 war 文件的名称>foo/bar
。因此,鉴于我发布的 pom.xml ,它变成了 http://localhost:8080/test-0.0.1-SNAPSHOT/foo/bar 。我讨厌 WAR 文件。
OK. I've solved the problem.
My application's path isn't at
http://localhost:8080/foo/bar
but athttp://localhost:8080/<the name of my war file>foo/bar
. So given thepom.xml
I've posted it becomeshttp://localhost:8080/test-0.0.1-SNAPSHOT/foo/bar
.I hate WAR files.
您有一个调用使用 @Path,而另一个调用则使用 @ApplicationPath,但方法上没有 @Path。
正如您可以此处,
但是您至少需要在被调用的方法上有一个 @Path 。然而,最简单的事情可能是从经典的旧 web.xml 开始,然后在资源上使用 @Path。您将在网络上找到大量示例,而 @ApplicationPath 并不常见。
You've got one call with @Path, while the other have @ApplicationPath with NO @Path on the method.
As you can here,
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.
虽然这不是您的问题,但如果您尝试在 Cargo 中的 Jetty 8 中部署,您可能会遇到此错误:http://jira.codehaus.org/browse/CARGO-1133
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