Jersey REST Web 服务、Tomcat、Eclipse 和 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17709039/
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 REST Web Service, Tomcat, Eclipse and 404's
提问by Jason Allen
I've read through a number of posts, but just can't seem to solve my problem. You'll also see tons of posts very similar to this one, even the same tutorial. Even following them, I can't seem to get to the answer.
我已经阅读了许多帖子,但似乎无法解决我的问题。您还会看到大量与此非常相似的帖子,甚至是同一个教程。即使跟随他们,我似乎也无法得到答案。
Essentially, I'm trying to follow the simple tutorial at: http://www.vogella.com/articles/REST/
本质上,我正在尝试按照以下简单教程进行操作:http: //www.vogella.com/articles/REST/
I've made a few changes to make it compatible with Jersey 2.x
我做了一些更改以使其与 Jersey 2.x 兼容
I'm using: Eclipse Tomcat 6 (Deployed/Run as within Eclipse) jaxrs-ri-2.0 I've enabled the JAX-RS Facet in Eclipse
我正在使用:Eclipse Tomcat 6(在 Eclipse 中部署/运行)jaxrs-ri-2.0 我在 Eclipse 中启用了 JAX-RS Facet
Everything builds fine Tomcat starts fine within Eclipse I can get to a static page content via:
一切都很好 Tomcat 在 Eclipse 中启动良好 我可以通过以下方式访问静态页面内容:
http://localhost:8080/RestTEST2/index.html
http://localhost:8080/RestTEST2/index.html
However, when I try to access my service via:
但是,当我尝试通过以下方式访问我的服务时:
http://localhost:8080/RestTEST2/jaxrs/hello
http://localhost:8080/RestTEST2/jaxrs/hello
I receive a 404 with "message not found" and "The requested resource (Not Found) is not available."
我收到 404 消息,其中包含“未找到消息”和“请求的资源(未找到)不可用”。
Here is my web.xml which is located at /WebContent/WEB-INF/web.xml
这是我的 web.xml,它位于 /WebContent/WEB-INF/web.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" 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>TestREST2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>TestREST</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
Here is my Java class:
这是我的 Java 类:
package TestREST;
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> ";
}
}
I also have a JAX-RS User Library configured and referenced that includes all the JAX-RS jars.
我还配置并引用了一个 JAX-RS 用户库,其中包含所有 JAX-RS jar。
Thoughts on what would cause the web service to not be found?
关于什么会导致找不到 Web 服务的想法?
采纳答案by Michal Gajdos
Jersey 2.0 does not recognize init-param with name com.sun.jersey.config.property.packages
(web.xml). Try to change it to jersey.config.server.provider.packages
as described in ServerProperties.PROVIDER_PACKAGES.
Jersey 2.0 无法识别带有名称com.sun.jersey.config.property.packages
(web.xml) 的init-param 。尝试将其更改jersey.config.server.provider.packages
为ServerProperties.PROVIDER_PACKAGES 中所述。
UPDATE (2020): try this link for current apidocs ServerProperties.PROVIDER_PACKAGES:
更新(2020 年):尝试当前 apidocs ServerProperties.PROVIDER_PACKAGES 的链接:
回答by Suren Konathala
Thanks a lot.. i've been fighting for it as well.
非常感谢..我也一直在为之奋斗。
This combination worked for me: Tomcat 7.0.55 Eclipse Luna Java 1.6 Jersey 1.7
这个组合对我有用:Tomcat 7.0.55 Eclipse Luna Java 1.6 Jersey 1.7
web.xml:
网页.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.trgr.cobalt.cmdb.jersey.resource;com.trgr.cobalt.cmdb.jersey.beans;com.trgr.cobalt.cmdb.elasticity</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>
回答by spt025
I would like to add one answer in this post.I was struggling with same problem for two days and couldn't find the solution. I tried all the possible solutions provided here but later on I realized that server was giving error of
我想在这篇文章中添加一个答案。我在同一个问题上苦苦挣扎了两天,但找不到解决方案。我尝试了这里提供的所有可能的解决方案,但后来我意识到服务器给出了错误
org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
This link answers gives the solution if somebody is stuck like me.. org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
如果有人像我一样被卡住,这个链接给出了解决方案.. org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
Actually first answer given in this post suggest the solution which is for jersey 2.x bundle and if you are using jersey 1.x then it will keep on giving error.
实际上,这篇文章中给出的第一个答案建议了针对 jersey 2.x 包的解决方案,如果您使用的是 jersey 1.x,那么它将继续给出错误。
Kindly refer to link given in answer for further reference
请参考答案中给出的链接以供进一步参考
回答by Sourav Purakayastha
Recently I found myself stuck in this problem. I have got it resolved by following these two points as the solution:
最近我发现自己陷入了这个问题。我通过以下两点作为解决方案解决了它:
- By making the
src/main/java
as the source directory, and keeping the Project Structure as shown below:
- 通过将
src/main/java
设为源目录,并保持项目结构如下所示:
- Changing
com.sun.jersey.config.property.packages
tojersey.config.server.provider.packages
in theweb.xml
- 更改
com.sun.jersey.config.property.packages
到jersey.config.server.provider.packages
在web.xml
回答by Arvind Choudhary
Simply by modifying the configuration of Apache Tomcat v7.0 while creating Dynamic Web Project, to include REST, I solved my problem. It is not enabled by default.
只需在创建动态 Web 项目时修改 Apache Tomcat v7.0 的配置,以包含 REST,我就解决了我的问题。默认情况下不启用。