错误 404 - 请求的资源不可用 - Eclipse Tomcat7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22355806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error 404 - The requested resource is not available- Eclipse Tomcat7
提问by Suvimo
I am running my project with TomCat7 with Eclipse under UBUNTU I am trying to follow a tutorial online to create a RESTful api. I follow exactly the same steps, but I get the 404 error when I try to run the project.
我在 UBUNTU 下使用 Eclipse 使用 TomCat7 运行我的项目我试图按照在线教程创建一个 RESTful api。我遵循完全相同的步骤,但是当我尝试运行该项目时出现 404 错误。
Here is my .class file that is in the src folder
这是我在 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>";
}
}
And this is web.xml that is in WEB-INF -> lib
这是在 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>
When I try to access this from localhost, I get the error..
当我尝试从本地主机访问它时,出现错误..
The requested resource (/RESTfulWS/) is not available.
请求的资源 (/RESTfulWS/) 不可用。
采纳答案by Simon Arsenault
Did you check the console / logs to make sure your web app deployed and started properly in tomcat?
您是否检查了控制台/日志以确保您的 Web 应用程序在 tomcat 中部署并正确启动?
Did you try the (GET) request /RESTfulWS/rest/UserInfoService/name/john
您是否尝试过(GET)请求 /RESTfulWS/rest/UserInfoService/name/john
回答by BlackJerry
For Jersey 2.x, the servlet class for ServletContainer should be "org.glassfish.jersey.servlet.ServletContainer", the param-name for packages should be "jersey.config.server.provider.packages".
对于 Jersey 2.x,ServletContainer 的 servlet 类应该是“org.glassfish.jersey.servlet.ServletContainer”,包的 param-name 应该是“jersey.config.server.provider.packages”。
Try
尝试
<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>