【问题标题】:Error 404 - The requested resource is not available- Eclipse Tomcat7错误 404 - 请求的资源不可用 - Eclipse Tomcat7
【发布时间】:2014-03-12 15:13:06
【问题描述】:

我在 UBUNTU 下使用带有 Eclipse 的 TomCat7 运行我的项目 我正在尝试按照在线教程创建一个 RESTful api。我遵循完全相同的步骤,但在尝试运行项目时出现 404 错误。

这是我在 src 文件夹中的 .class 文件

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-INF -> lib 中的 web.xml

<?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/) 不可用。

【问题讨论】:

  • 您在哪个上下文路径中部署此 Web 应用程序?
  • @MarkThomas,我正在 /opt/tomcat 中部署 Tomcat 的安装文件夹。当我转到 config/server.xml 时,我可以看到我的应用程序,其中:&lt;Context docBase="/opt/tomcat/wtpwebapps/RESTfulWS" path="/RESTfulWS" reloadable="true" source="org.eclipse.jst.jee.server:RESTfulWS"/&gt;
  • 如果你请求 /RESTfulWS/rest/ ?
  • 不幸的是一样。

标签: eclipse web-services tomcat tomcat7 web.xml


【解决方案1】:

您是否检查了控制台/日志以确保您的 Web 应用程序已在 tomcat 中正确部署和启动?

您是否尝试过 (GET) 请求 /RESTfulWS/rest/UserInfoService/name/john

【讨论】:

  • 愚蠢的是,我只请求 /restfulWS 而没有 URL 的其余部分!主要是因为我试图在 Eclipse 中运行该项目,它只指向 /RESTfulWS!谢谢!:)
【解决方案2】:

对于 Jersey 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> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2014-01-31
    • 2012-09-12
    相关资源
    最近更新 更多