【发布时间】:2026-01-22 10:35:01
【问题描述】:
我正在尝试使用 eclipse/Tomcat/Jersey 制作一个简单的 Rest WS,但我无法使其工作。这令人沮丧,因为我没有看到错误,但似乎真的很微妙。
这是我的 web.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" 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>LearnWS</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>com.example</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>
这是 Hello.java:
package com.example;
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 Hello {
// 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>";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
如你所见,是典型的hello world。但对我来说是行不通的。
我导出 .war 文件,部署在 Tomcat Web 应用程序目录,打开管理器(那里是“学习”),但是当我尝试 http://localhost:8080/Learn/rest/hello 时,我只看到 请求的资源不可用强>错误。在图片中您看不到它,但该项目名为 Learn。
在 catalina.out 中部署没有错误。
我正在使用 Java SE 6(1.6.0_65-b14-462 用于构建)和 Tomcat 7.0.54(JVM 1.8.0_05-b13)。也许这就是问题所在,但不知道。
【问题讨论】:
标签: java web-services rest tomcat jersey-2.0