java Jersey hello world 给出 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10519862/
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
Jersey hello world gives 404
提问by cyberbemon
I have the following code in my java class
我的java类中有以下代码
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
//This method is called is TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
return "Hello World";
}
//this method is called if TEXT_XML is requested
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return "<?xml version=\"1.0\"?>"+"<Hello> Hello World"+"</hello>";
}
//this method is called if HTML is requested
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(){
return "<html>"+"<title>"+"hello jersey"+"</title>"+"<body><h1>"+"hello World!!"+"</body></h1>"+"</html>";
}
}
I compiled it and exported it as a .WAR file , when I type
我编译它并将其导出为 .WAR 文件,当我输入时
I get a 404 . I tried it in WTP, cURL they all return 404.. I'm using tomcat 7.0.26
我得到一个 404 。我在 WTP 中尝试过,cURL 他们都返回 404 .. 我正在使用 tomcat 7.0.26
Note: I am running Tomcat on port 80, and other servlets respond as expected.
注意:我在端口 80 上运行 Tomcat,其他 servlet 会按预期响应。
web.xml config
web.xml 配置
<display-name>Jersey_Test</display-name>
<servlet>
<servlet-name>Hello</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.example.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/*</url-pattern>
The following URL gives me Http status 500
下面的 URL 给了我 Http status 500
http://localhost/Jersey_Test/rest/hello
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
采纳答案by cyberbemon
The problem has been fixed and this is how I did it.
问题已解决,我就是这样做的。
I removed the jersey .jar files from the build path and replaced them in the WEB-INF\lib
folder and everything worked.
我从构建路径中删除了 jersey .jar 文件并将它们替换到WEB-INF\lib
文件夹中,一切正常。
回答by BalusC
That can happen if you haven't registered the JAX-RS servlet implementation in your web.xml
at all. Jersey requires the following configuration:
如果您根本没有注册 JAX-RS servlet 实现,就会发生这种情况web.xml
。Jersey 需要如下配置:
<servlet>
<servlet-name>jersey</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.example.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The com.sun.jersey.config.property.packages
initialization parameter value must point to the package where all your services are. Your code snippet is however missing the package
declaration. I'm not sure if this is omitted for brevity or not, but packageless classes are invisible to classes which are by itself in a package (such as the Tomcat and Jersey engines itself). The above web.xml
example assumes that you've a
该com.sun.jersey.config.property.packages
初始化参数值必须指向你所有的服务包。但是,您的代码片段缺少package
声明。我不确定是否为了简洁而省略了这一点,但无包类对于包中的类(例如 Tomcat 和 Jersey 引擎本身)是不可见的。上面的web.xml
例子假设你有一个
package com.example.service;
on your webservice classes. Fix or change it accordingly.
在您的网络服务类上。相应地修复或更改它。
Note that the URL-pattern of /*
thus means that ALL requests will be passed through Jersey. If you need to deploy other servlets, JSP or static content in the same webapp as well, you might want to specify a more specific URL pattern. E.g.
请注意,因此的 URL 模式/*
意味着所有请求都将通过 Jersey。如果您还需要在同一个 web 应用程序中部署其他 servlet、JSP 或静态内容,您可能需要指定更具体的 URL 模式。例如
<url-pattern>/rest/*</url-pattern>
You'll only need to change your request URL to http://localhost/test_server/rest/hello.
您只需将请求 URL 更改为http://localhost/test_server/rest/hello。
回答by Oleksi
Looks like you are registering your servlet in the wrong place. Double check that the root URL of your servlet, and make sure that matches what you are hitting.
看起来您在错误的位置注册了 servlet。仔细检查您的 servlet 的根 URL,并确保与您正在点击的内容匹配。
Have you tried hitting?:
你试过打吗?:
http://127.0.0.1/hello
Remember that /hello
will go after whatever the base URL of your servlet is. Try looking a it in a debugger to see where it mounts.
请记住,/hello
无论您的 servlet 的基本 URL 是什么,它都会被处理。尝试在调试器中查看它以查看它的安装位置。
回答by abhinavroy23
For me, there was a jar
file added in Build path -> Libraries
for which the actual jar
was missing from the file system.
对我来说,jar
添加了一个文件,文件系统中缺少Build path -> Libraries
实际jar
文件。
I removed the entry from build path
and added the dependency in pom.xml
.
worked like a charm.
我删除了条目build path
并在pom.xml
. 像魅力一样工作。