使用 Tomcat 的简单 RESTeasy 示例的 404 响应
我正在尝试使用 Tomcat 和 RESTeasy 组合一个非常简单的“hello world”服务。但是当我尝试测试它时,我得到的只是 Tomcat 的 404 响应。以下是我遵循的步骤,希望有人能指出我出错的地方:
在 Eclipse 中创建了一个新的动态 Web 项目。目标运行时设置为 Apache Tomcat 7.0、动态 Web 模块版本 3.0。
将resteasy-jaxrs-2.3.1.GA-all.zip中的所有jar文件复制到WEB-INF\lib
添加了一个类:
package com.eshayne.resteasy;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/sampleservice")
public class SampleService {
@GET
@Path("/hello")
@Produces(MediaType.TEXT_HTML)
public String hello()
{
return "hello world";
}
}
- 将
web.xml
设置为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
<display-name>resteasy</display-name>
<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>/restful-services/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/restful-services</param-value>
</context-param>
</web-app>
将新项目添加到 Tomcat 并重新启动Tomcat(在 Eclipse 中)
打开 Web 浏览器并请求 /resteasy/restful-services/sampleservice/hello
这会从 Tomcat 返回 404 响应,其描述为:
请求的资源(/resteasy/restful-services/sampleservice/hello)不可用。
我缺少什么?
I am trying to put together a very simple "hello world" service with Tomcat and RESTeasy. But when I try to test it, all I get are 404 responses from Tomcat. Here are the steps I followed, hopefully someone can point out where I went wrong:
Created a new Dynamic Web Project in Eclipse. The Target Runtime is set for Apache Tomcat 7.0, Dynamic web module version 3.0.
Copied all of the jar files from resteasy-jaxrs-2.3.1.GA-all.zip into WEB-INF\lib
Added one class:
package com.eshayne.resteasy;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/sampleservice")
public class SampleService {
@GET
@Path("/hello")
@Produces(MediaType.TEXT_HTML)
public String hello()
{
return "hello world";
}
}
- Set
web.xml
to:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
<display-name>resteasy</display-name>
<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>/restful-services/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/restful-services</param-value>
</context-param>
</web-app>
Added the new project to Tomcat and restarted Tomcat (within Eclipse)
Opened a web browser and requested /resteasy/restful-services/sampleservice/hello
This returns a 404 response from Tomcat with the description:
The requested resource (/resteasy/restful-services/sampleservice/hello) is not available.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过访问 URL 的变体 - 例如 /restful-services/sampleservice/hello
Have you tried visiting variations on the URL - e.g. /restful-services/sampleservice/hello
事实证明,给定资源的 URL 实际上不是
///
。它实际上是///
。It turns out that the URL for a given resource is not in fact
<display-name>/<servlet-mapping>/<class Path>/<method Path>
. It is actually<project-name>/<servlet-mapping>/<class Path>/<method Path>
.引用RESTEasy教程:访问资源的URL由
For例如,如果您部署
demo.war
,它看起来像这样:并且
web.xml
中必须有一个servlet-mapping
,例如:A quote from RESTEasy tutorial: The URL to access a resource consists of
For example, if you deploy
demo.war
, it looks like this:And there must be a
servlet-mapping
inweb.xml
, for example: