还有其他人问这个问题,但是他们得到的答案对我没有用。我正在运行Tomcat 7 中此链接中所讨论的Jersey休息服务器。我的Resource类是Hello,如下所示:
package com.rest.videos; 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>"; } // 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_3_0.xsd" version="3.0"> <display-name>Videos</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>ServletAdaptor</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.rest.videos</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletAdaptor</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
每当我访问URL时http://localhost:8080/Videos/rest/hello,它都返回404。我的WEB- INF目录中有一个index.html文件,如果我转到http://localhost:8080/Videos/应该的位置,因为welcome- file-list这样说,我也得到404。
http://localhost:8080/Videos/rest/hello
http://localhost:8080/Videos/
我究竟做错了什么?
好吧,如果您不介意提供积分,我会发布答案的:)
web.xml与URI路径无关,因此,除非将您的WAR包装到带有/ Videos的EAR中,否则此前缀无效,并且很可能是404的根。