我的 java 类中有以下代码
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
//This method is called is TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
return "Hello World";
}
//this method is called if TEXT_XML is requested
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return "<?xml version=\"1.0\"?>"+"<Hello> Hello World"+"</hello>";
}
//this method is called if HTML is requested
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(){
return "<html>"+"<title>"+"hello jersey"+"</title>"+"<body><h1>"+"hello World!!"+"</body></h1>"+"</html>";
}
}
当我键入时,我编译它并将其导出为 .WAR 文件
我得到一个 404 。我在 WTP 中尝试过,cURL 他们都返回 404 .. 我使用的是 tomcat 7.0.26
注意:我在端口 80 上运行 Tomcat,其他 servlet 会按预期响应。
web.xml 配置
<display-name>Jersey_Test</display-name>
<servlet>
<servlet-name>Hello</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.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/*</url-pattern>
以下 URL 给了我 Http status 500
http://localhost/Jersey_Test/rest/hello
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer