Java 休息泽西雄猫 7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21380084/
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
REST Jersey Tomcat 7
提问by user3131119
Getting 404 error, when accessing the
访问时出现 404 错误
Hello.java
你好.java
package de.vogella.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// Plain old Java Object it does not extend as class or implements
// an interface
// The class registers its methods for the HTTP GET request using the @GET annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.
// The browser requests per default the HTML MIME type.
//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
'
web.xml
网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>de.vogella.jersey.first</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>de.vogella.jersey.first</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>
This has been taken from the http://www.vogella.com/tutorials/REST/article.html, but when I run the
http://localhost:8080/de.vogella.jersey.first/rest/hello
这取自http://www.vogella.com/tutorials/REST/article.html,但是当我运行
http://localhost:8080/de.vogella.jersey.first/rest/hello
. I get 404 error .
. 我收到 404 错误。
Can someone please help me on this.
有人可以帮我解决这个问题。
回答by G. Demecki
I've completed this tutorial, and successfully launched your example.
我已经完成了本教程,并成功启动了您的示例。
My tip: tutorial you are trying to complete is 4-years old, so is not so actual. Better download full working example with jersey 2.0.
我的提示:您尝试完成的教程已有 4 年的历史,因此不太实际。更好地下载带有 jersey 2.0 的完整工作示例。
Your possible errors:
您可能的错误:
- Firstly you need
jersey-archive-1.16
version. Jersey's new version
no longer contains classcom.sun.jersey.spi.container.servlet.ServletContainer
which you are refering to inside web.xml - Secondly: make sure your application's context-rootis really set to
de.vogella.jersey.first
. If you're using eclipse check file:org.eclipse.wst.common
and itscontext-root
parameter. - And of course make sure your project compiles correctly and on the classpath are at least four jars: asm.jar, jersey-core.jar, jersey-server.jar, jersey-servlet.jar.
- 首先你需要
jersey-archive-1.16
版本。Jersey 的新版本
不再包含com.sun.jersey.spi.container.servlet.ServletContainer
您在web.xml中引用的类 - 其次:确保您的应用程序的上下文根确实设置为
de.vogella.jersey.first
. 如果您使用的是 eclipse 检查文件:org.eclipse.wst.common
及其context-root
参数。 - 当然,请确保您的项目正确编译,并且类路径上至少有四个 jar:asm.jar、jersey-core.jar、jersey-server.jar、jersey-servlet.jar。
回答by Barani r
I am just providing more info over Grzesiek D, which helped me fix ths issue.
我只是提供有关 Grzesiek D 的更多信息,这帮助我解决了这个问题。
jersey-archive-1.16 version. Jersey's new version no longer contains class com.sun.jersey.spi.container.servlet.ServletContainer which you are refering to inside web.xml
make sure your application's context-root is really set to de.vogella.jersey.first. If you're using eclipse check file: org.eclipse.wst.common and its context-root parameter.
jersey-archive-1.16 版本。Jersey 的新版本不再包含类 com.sun.jersey.spi.container.servlet.ServletContainer,您在 web.xml 中引用该类
确保您的应用程序的上下文根确实设置为 de.vogella.jersey.first。如果您使用 eclipse 检查文件:org.eclipse.wst.common 及其上下文根参数。
***To change the context-rrot to match the package of your project:
***要更改 context-rrot 以匹配您的项目包:
(i) In your project's Properties, choose "Web Project Settings".
(i) 在您项目的属性中,选择“Web 项目设置”。
(ii) Change "Context root" to "app". screen shot of Eclipse project properties Web Project Settings
(ii) 将“上下文根”更改为“应用程序”。Eclipse 项目属性 Web 项目设置的屏幕截图
(iii) Choose Window > Show View > Servers.
(iii) 选择窗口 > 显示视图 > 服务器。
(iv) Stop the server by either clicking the red square box ("Stop the server" tooltip) or context-click on the server listing to choose "Stop".
(iv) 通过单击红色方框(“停止服务器”工具提示)或上下文单击服务器列表以选择“停止”来停止服务器。
(v)On the server you want to use, context-click to choose "Clean…"
(v) 在您要使用的服务器上,右键单击以选择“清洁...”
(vi) Click OK in this confirmation dialog box. ***************
(vi) 在此确认对话框中单击确定。***************
- And of course make sure your project compiles correctly and on the classpath are at least four jars: asm.jar, jersey-core.jar, jersey-server.jar, jersey-servlet.jar.
- 当然,请确保您的项目正确编译,并且在类路径上至少有四个 jar:asm.jar、jersey-core.jar、jersey-server.jar、jersey-servlet.jar。