我正在使用TomCat7和UBUNTU下的Eclipse运行我的项目 我正在尝试在线学习教程以创建RESTful API。我遵循完全相同的步骤,但是当我尝试运行项目时遇到404错误。
这是我的.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-INF中的web.xml - &gt; 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 :(得分:0)
您是否检查了控制台/日志,以确保您的网络应用已在tomcat中正确部署和启动?
您是否尝试过(GET)请求/ RESTfulWS / rest / UserInfoService / name / john
答案 1 :(得分:0)
对于Jersey 2.x,ServletContainer的servlet类应为“org.glassfish.jersey.servlet.ServletContainer”,包的param-name应为“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>