我在JAVA中编写了一个简单的RESTful Web服务,但是当我使用 Run As - >运行它时从Eclipse IDE运行服务器选项,我收到HTTP Status 404 – Not Found
错误。请告诉我代码中的问题是什么?
的web.xml
<?xml version="1.0" encoding="UTF-8"?> <!--?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>RESTfulWebServiceExample</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>org.arpit.javapostsforlearning.webservice</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>
FeetToInchAndInchToFeetConversionService.java
package org.arpit.javapostsforlearning.webservice;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("ConversionService")
public class FeetToInchAndInchToFeetConversionService {
@GET
@Path("/InchToFeet/{i}")
@Produces(MediaType.TEXT_XML)
public String convertInchToFeet(@PathParam("i") int i) {
int inch=i;
double feet = 0;
feet =(double) inch/12;
return ""
+ "" + inch + ""
+ "" + feet + ""
+ "";
}
@Path("/FeetToInch/{f}")
@GET
@Produces(MediaType.TEXT_XML)
public String convertFeetToInch(@PathParam("f") int f) {
int inch=0;
int feet = f;
inch = 12*feet;
return ""
+ "" + feet + ""
+ "" + inch + ""
+ "";
}
}
答案 0 :(得分:2)
根据您的localhost:8080/RESTfulWebServiceExample
, RESTfulWebServiceExample 是您的显示名称,您尝试转到web.xml
。您需要转到localhost:8080/rest/
来命中您的servlet上下文根(如web.xml
的servlet-mapping部分中所定义)。