RESTEasy - 需要完整路径的@Path?
我正在搞乱JAX-RS并创建了一个调用生成JSON的REST服务的应用程序。我试过Jersey,一切都很顺利,但我不得不切换到RESTEasy,因为我的应用程序需要用JDK5构建。我将我的web.xml更改为以下内容:
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:
<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>
所以我希望每个以/ rest开头的URL都由RESTEasy处理。我的服务如下:
So I expect every URL starting with /rest to be handled by RESTEasy. My services are as follows:
@Path("/services")
public class MyRESTServices {
@GET
@Path("service1")
@Produces(MediaType.APPLICATION_JSON)
public Object service1(Blah blah) {
}
}
使用Jersey工作正常, http:// localhost / MyContext / rest / services / service1 绑定到我的service1()方法。但是当我改为RESTEasy时,我有一个404:
This worked fine using Jersey, http://localhost/MyContext/rest/services/service1 was bound to my service1() method. When I change to RESTEasy, though, I had a 404:
HTTP状态404 - 无法找到相对的资源:/ rest / services / service1 of full path: http:// localhost / MyContext / rest / services / service1
这意味着RESTEasy处理了请求但找不到绑定到此URL的任何服务。
Which means that RESTEasy handled the request but could not find any service bound to this URL.
在我的课上,将 @Path(/ services)
更改为 @Path(/ rest / services)$但是,c $ c>工作了。你知道我为什么会有这种奇怪的行为吗?我读过的所有教程/文档都只提到相对路径,不包括/ rest前缀...
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...
解决方案:添加在您的web.xml中关注
Solution: add the following in your web.xml
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
其中/ rest是< url-pattern> /的开头rest / *< / url-pattern>
(来源: http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/ Installation_Configuration.html#d0e72 )