【发布时间】:2012-05-17 02:48:19
【问题描述】:
这是我第一次使用 Eclipse,这让我非常愤怒。
我安装了 Tomcat 6.0,下载了 Jersey 库,并按照以下教程进行操作:http://www.vogella.com/articles/REST/article.html#first_client
我将项目名称创建为 RestExample,在其中我有一个包 de.jay.jersey.first,在其中我有一个类 HelloWorldResource,如下所示:
package de.jay.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class HelloWorldResource {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
我的 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>RestExample</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>de.jay.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我正在尝试使用 curl 作为:
卷曲http://localhost:8081/RestExample/rest/hello
Apache Tomcat/6.0.35 - 错误报告
HTTP 状态 404 - /RestExample/rest/Hello
类型状态重新 港口
消息 /RestExample/rest/hello
de 说明 请求的资源(/RestExample/rest/hello)是 不可用。
Apache Tomcat/6.0.35问题是我应该在 web.xml 中更改什么以便我可以访问该资源?
我试过 RestExample/de.jay.jersey.first/rest/hello,但还是不行。 TOmcat 运行正常。
【问题讨论】:
-
您是如何将此应用程序部署到 Tomcat 的?具体来说,你的 web.xml 在哪里,你的类文件在哪里?
-
我试过 Tomcat 7.0,它运行良好
-
我只将 web.xml 中的 Servlet 版本从 2.5 更改为 3.0 之类的小部分
-
@MarkThomas:web.xml 位于 RestExample\WebContent\WEB-INF\,类文件位于 RestExample\build\classes\de\jay\jersey\first\
-
类需要位于 WEB-INF/classes 下,因此在本例中为 WEB-INF/classes/de/jay/jersey/first/HelloWorldResource。请注意,如果您通过 Eclipse 进行部署,它可能会在后台为您执行此操作。正在发生的事情的透明度是我更喜欢让 Eclipse 构建 WAR 并自己部署它的原因之一。
标签: java eclipse web-services rest tomcat