2014-03-12 23 views
1

我UBUNTU 下運行我的TomCat7與Eclipse項目我試圖遵循在線教程創建一個RESTful API。我遵循完全相同的步驟,但是當我嘗試運行該項目時遇到了404錯誤。錯誤404 - 請求的資源不是服務現有的Eclipse Tomcat7

這裏是我的.class文件是在src文件夾

package com.eviac.blog.restws; 




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; 




/** 
* 
* @author pavithra 
* 
*/ 




// @Path here defines class level path. Identifies the URI path that 
// a resource class will serve requests for. 
@Path("UserInfoService") 
public class UserInfo { 




// @GET here defines, this method will method will process HTTP GET 
// requests. 
@GET 
// @Path here defines method level path. Identifies the URI path that a 
// resource class method will serve requests for. 
@Path("/name/{i}") 
// @Produces here defines the media type(s) that the methods 
// of a resource class can produce. 
@Produces(MediaType.TEXT_XML) 
// @PathParam injects the value of URI parameter that defined in @Path 
// expression, into the method. 
public String userName(@PathParam("i") String i) { 




String name = i; 
return "<User>" + "<Name>" + name + "</Name>" + "</User>"; 
} 




@GET 
@Path("/age/{j}") 
@Produces(MediaType.TEXT_XML) 
public String userAge(@PathParam("j") int j) { 




int age = j; 
return "<User>" + "<Age>" + age + "</Age>" + "</User>"; 
} 
} 

這是web.xml文件是在WEB-INF - > lib中

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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>RESTfulWS</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>com.eviac.blog.restws</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> 

當我嘗試訪問這從localhost,我得到錯誤..

所請求的資源(/ RESTfulWS /)不可用。

+0

在什麼情況下路徑在部署這個Web應用程序? –

+0

@MarkThomas,我的Tomcat部署的安裝文件夾中的/ opt/tomcat的。當我去的config/server.xml中,我可以看到我的應用程序,其中: '<上下文的docBase =「/選擇/ tomcat的/ wtpwebapps/RESTfulWS」路徑= 「/ RESTfulWS」 重新加載= 「真實」 來源= 「org.eclipse.jst.jee.server:RESTfulWS」/>' – Suvimo

+0

如果你請求/ RESTfulWS/REST /? –

回答

0

您是否檢查控制檯/日誌以確保您的Web應用程序在tomcat中正確部署和啓動?

你有沒有嘗試(GET)請求/ RESTfulWS/REST/UserInfoService /名/約翰

+0

愚蠢的是,我只請求/ restfulWS而沒有其餘的URL!主要是因爲我試圖在eclipse中運行該項目,並且它僅指向/ RESTfulWS! 謝謝!:) – Suvimo

0

對於新澤西2.x中,對於ServletContainer servlet類應該是 「org.glassfish.jersey.servlet.ServletContainer」 ,包的參數名應該是「jersey.config.server.provider.packages」。

嘗試

<servlet> 
<servlet-name>Jersey REST Service</servlet-name> 
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
<init-param> 
<param-name>jersey.config.server.provider.packages</param-name> 
<param-value>com.eviac.blog.restws</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 
相關問題