泽西 tomcat 和日食:错误 404

Jersey tomcat and eclipse: error 404

我在关注 vogella "REST with Java (JAX-RS) using Jersey - Tutorial"。

我创建了一个名为 "project" 的新动态 Web 项目,生成 web.xml 文件。

我在 "eclipseWorkspace/project/WebContent/WEB-INF/lib"

中添加了来自 jaxrs-ri-2.23.1 的所有 jar 文件

我创建了一个名为 Hello 的新 class 并从 vogella 教程中复制了内容:

package project;

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 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>project</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>project</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>

右键单击项目名称,运行 as,运行 on server。在本地主机上选择了现有的 tomcat v7 服务器。

重新启动服务器。

当我去:

http://localhost:8080/project/rest/hello

显示404错误:

HTTP Status 404 - Not Found

type Status report

message Not Found

description The requested resource is not available.

Apache Tomcat/7.0.70

你知道怎么解决吗?

解决了在 eclipse 中取消选择选项 "Build Automatically" 的问题。

然后 "Build project" 和 "Run on server" 就可以了。