【发布时间】: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 时,我可以看到我的应用程序,其中:
<Context docBase="/opt/tomcat/wtpwebapps/RESTfulWS" path="/RESTfulWS" reloadable="true" source="org.eclipse.jst.jee.server:RESTfulWS"/> -
如果你请求 /RESTfulWS/rest/ ?
-
不幸的是一样。
标签: eclipse web-services tomcat tomcat7 web.xml