【问题标题】:Jersey tomcat and eclipse: error 404Jersey Tomcat 和 Eclipse:错误 404
【发布时间】:2016-11-21 00:19:45
【问题描述】:

我正在关注 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 的新类并复制了 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>

右击项目名称,运行方式,在服务器上运行。在 localhost 选择了现有的 tomcat v7 服务器。

重新启动服务器。

当我去:

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

显示404错误:

HTTP 状态 404 - 未找到

输入状态报告

找不到消息

说明请求的资源不可用。

Apache Tomcat/7.0.70

你知道怎么解决吗?

【问题讨论】:

    标签: eclipse tomcat jersey http-status-code-404


    【解决方案1】:

    解决了在 Eclipse 中取消选择“自动构建”选项的问题。

    然后“构建项目”和“在服务器上运行”,它就可以工作了。

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2013-07-16
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多