Jersey REST Web 服务、Tomcat、Eclipse 和 404 [英] Jersey REST Web Service, Tomcat, Eclipse and 404's
问题描述
我已经阅读了许多帖子,但似乎无法解决我的问题.您还会看到大量与此非常相似的帖子,甚至是同一个教程.即使跟着他们,我似乎也无法得到答案.
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.
基本上,我正在尝试遵循以下简单教程:http://www.vogella.com/articles/REST/
Essentially, I'm trying to follow the simple tutorial at: http://www.vogella.com/articles/REST/
我进行了一些更改以使其与 Jersey 2.x 兼容
I've made a few changes to make it compatible with Jersey 2.x
我正在使用:蚀Tomcat 6(在 Eclipse 中部署/运行)jaxrs-ri-2.0我在 Eclipse 中启用了 JAX-RS Facet
I'm using: Eclipse Tomcat 6 (Deployed/Run as within Eclipse) jaxrs-ri-2.0 I've enabled the JAX-RS Facet in Eclipse
一切都很好Tomcat 在 Eclipse 中正常启动我可以通过以下方式访问静态页面内容:
Everything builds fine Tomcat starts fine within Eclipse I can get to a static page content via:
http://localhost:8080/RestTEST2/index.html
但是,当我尝试通过以下方式访问我的服务时:
However, when I try to access my service via:
http://localhost:8080/RestTEST2/jaxrs/hello
我收到 404 消息,其中包含未找到消息"和请求的资源(未找到)不可用."
I receive a 404 with "message not found" and "The requested resource (Not Found) is not available."
这是我的 web.xml,它位于/WebContent/WEB-INF/web.xml
Here is my web.xml which is located at /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>
这是我的 Java 类:
Here is my Java class:
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> ";
}
}
我还配置并引用了一个 JAX-RS 用户库,其中包含所有 JAX-RS jar.
I also have a JAX-RS User Library configured and referenced that includes all the JAX-RS jars.
关于什么会导致找不到 Web 服务的想法?
Thoughts on what would cause the web service to not be found?
推荐答案
Jersey 2.0 无法识别名称为 com.sun.jersey.config.property.packages
(web.xml) 的 init-param.尝试将其更改为 jersey.config.server.provider.packages
,如 ServerProperties.PROVIDER_PACKAGES.
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.
更新(2020 年):尝试当前 apidoc 的此链接 ServerProperties.PROVIDER_PACKAGES:
UPDATE (2020): try this link for current apidocs ServerProperties.PROVIDER_PACKAGES:
这篇关于Jersey REST Web 服务、Tomcat、Eclipse 和 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!