Java RESTEasy - @Path 需要完整路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4131968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RESTEasy - @Path requiring a full path?
提问by Bastien Jansen
I was messing around with JAX-RS and made an application which calls REST services which produce JSON. I tried Jersey and everything went fine, but I had to switch to RESTEasy as my application needs to be built with JDK5. I changed my web.xml to something like this:
我正在使用 JAX-RS 并制作了一个应用程序,该应用程序调用生成 JSON 的 REST 服务。我尝试了 Jersey 并且一切顺利,但我不得不切换到 RESTEasy,因为我的应用程序需要使用 JDK5 构建。我把我的 web.xml 改成这样:
<web-app>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>RESTEasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RESTEasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- ... -->
</web-app>
So I expect every URL starting with /rest to be handled by RESTEasy. My services are as follows:
所以我希望每个以 /rest 开头的 URL 都由 RESTEasy 处理。我的服务如下:
@Path("/services")
public class MyRESTServices {
@GET
@Path("service1")
@Produces(MediaType.APPLICATION_JSON)
public Object service1(Blah blah) {
}
}
This worked fine using Jersey, http://localhost/MyContext/rest/services/service1was bound to my service1() method. When I change to RESTEasy, though, I had a 404:
这使用 Jersey 运行良好,http://localhost/MyContext/rest/services/service1绑定到我的 service1() 方法。但是,当我更改为 RESTEasy 时,出现了 404:
HTTP Status 404 - Could not find resource for relative : /rest/services/service1 of full path: http://localhost/MyContext/rest/services/service1
HTTP 状态 404 - 找不到相对资源:/rest/services/service1 的完整路径:http://localhost/MyContext/rest/services/service1
Which means that RESTEasy handled the request but could not find any service bound to this URL.
这意味着 RESTEasy 处理了请求,但找不到绑定到此 URL 的任何服务。
On my class, changing @Path("/services")
to @Path("/rest/services")
worked, though. Do you have any idea why I got this strange behaviour? All the tutorials/docs I read mentionned only relative paths, not including the /rest prefix...
不过,在我的课堂上,@Path("/services")
改为@Path("/rest/services")
工作。你知道我为什么会有这种奇怪的行为吗?我读过的所有教程/文档都只提到了相对路径,不包括 /rest 前缀...
采纳答案by Bastien Jansen
Solution: add the following in your web.xml
解决方案:在您的 web.xml 中添加以下内容
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
Where /rest is the beginning of your <url-pattern>/rest/*</url-pattern>
/rest 是你的开始 <url-pattern>/rest/*</url-pattern>
(Source: http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/Installation_Configuration.html#d0e72)
(来源:http: //docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/Installation_Configuration.html#d0e72)
回答by user2276251
On JBoss AS 7.1 I also had to add to add resteasy.resources ... which is further explained here http://www.javaroots.com/2013/05/creating-rest-services-with-rest-easy.htmlYou may get error like this : Could not find resource for relative : /application/test of full path:... You have to define resteasy.resource context param with the full path of Rest class.
在 JBoss AS 7.1 上,我还必须添加以添加 resteasy.resources ... 这里进一步解释 http://www.javaroots.com/2013/05/creating-rest-services-with-rest-easy.html你可能会出现这样的错误:找不到相对资源:/application/test of full path:... 您必须使用 Rest 类的完整路径定义 resteasy.resource 上下文参数。