1

还有其他人问这个问题,但他们得到的答案对我不起作用。我正在运行一个 Jersey 休息服务器,如 Tomcat 7 中的此链接中所述。我的资源类是 Hello,如下所示:

package com.rest.videos;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@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 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_3_0.xsd"
    version="3.0">
    <display-name>Videos</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>ServletAdaptor</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.rest.videos</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

每当我访问 URLhttp://localhost:8080/Videos/rest/hello时,它都会返回 404。我的 WEB-INF 目录中有一个 index.html 文件,如果我去http://localhost:8080/Videos/我认为它应该在的位置,因为欢迎文件列表是这样说的,我也会得到一个 404。

我究竟做错了什么?

4

2 回答 2

1

好的,如果您不介意给予学分,我会发布答案:)

web.xml 与 URI 路径无关,因此除非将您的 WAR 包装到带有 /Videos 的 EAR 中,否则此前缀是无效的,并且可能是 404 的根目录。

于 2012-10-10T23:45:57.597 回答
0

Based on the tutorial you referred to, it seems you should try the following:
http://localhost:8080/com.rest.videos/rest/hello

Notice the base URI the client code is using?

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
 }
  • com.rest.videos refers to the package name
  • /rest refers to the servlet-mapping in web.xml
  • /hello refers to the @Path annotation
于 2012-10-11T00:13:48.860 回答