2017-10-11 79 views
-2

開始我已經用JAVA寫的一個簡單的RESTful Web服務,但是當我運行它使用運行方式 - >從Eclipse IDE的服務器選項來運行,我得到HTTP Status 404 – Not Found錯誤。請告訴我的代碼中可能存在什麼問題?RESTful Web服務並不在Eclipse IDE

響應: click here see the image showing server response

的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時,根據您的web.xml,RESTfulWebServiceExample是您的顯示名稱。你需要去localhost:8080/rest/命中你的servlet上下文根(如你的servlet-mapping部分中定義的web.xml)。