[英]Restful Web Service returning 404, bad web.xml?
還有其他人問這個問題,但是他們得到的答案對我沒有用。 我正在運行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。
我究竟做錯了什么?
好吧,如果您不介意提供積分,我會發布答案的:)
web.xml與URI路徑無關,因此,除非將您的WAR包裝到帶有/ Videos的EAR中,否則此前綴無效,並且很可能是404的根。
根據您所參考的教程,您似乎應該嘗試以下操作:
http://localhost:8080/com.rest.videos/rest/hello
注意客戶端代碼正在使用的基本URI嗎?
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
}
web.xml
的servlet-mapping
@Path
注釋
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.