【问题标题】:404 when accessing Jersey app in Tomcat 7在 Tomcat 7 中访问 Jersey 应用程序时出现 404
【发布时间】:2013-05-30 20:44:59
【问题描述】:

我是 web 开发的新手,我正在努力使用 jersey 创建一个简单的 rest web 服务,打包为一个独立的 war 文件,部署在 tomcat 7 上。

I have followed this tutorial 以创建简单的 hello world restful web 服务。 我使用 intelij 来创建 war 文件(war 文件和它的分解版本)并将它们部署在 tomcat\webapps 文件夹中。

启动 tomcat 时,我无法访问 url (http://localhost:8080/rest/hello) - 得到 404。

查看 tomcat 的日志,我找不到任何异常或错误,除了这些下一行:

INFO: Scanning for root resource and provider classes in the packages:
  sample
εαΘ 30, 2013 11:24:38 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class sample.Hello
εαΘ 30, 2013 11:24:38 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.

我已经搜索了很多并尝试遵循此错误的所有解决方案,但没有任何效果。

由于我的新手,我觉得我错过了一些非常基本的东西。 任何想法要搜索什么?我错过了什么? 这是一个简单的 hello world 应用程序,我感到非常沮丧和沮丧......

Java 类

package sample;

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 requested
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayHelloInPlainText() {
    return "Hello world!";
  }

  // This method is called if HTML is requested
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHelloInHtml() {
    return "<html> " + "<title>" + "Hello world!" + "</title>"
        + "<body><h1>" + "Hello world!" + "</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"
    id="WebApp_ID" version="3.0">

      <display-name>rest</display-name>

      <servlet>
        <servlet-name>Jersey REST Service</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>sample</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>

    </web-app>

【问题讨论】:

  • 请在您的问题中包含Hello.javaweb.xml 的代码。如果没有更多细节,很难为您提供帮助。
  • 您的 WAR 是否部署为 rest

标签: java rest tomcat jersey


【解决方案1】:

这与此 SO 问题相似:“No provider classes found: when running Jersey REST example application”。

“未找到提供程序类”的消息很好,无需担心。要使 URL http://localhost:8080/rest/hello 工作,您的 WAR 文件需要称为 rest.war。然后rest 成为上下文根。您在项目注释中定义的所有路径相对于上下文根

【讨论】:

  • 谢谢。毕竟,这确实是我的问题。 (缺少网址)。我的战争文件被称为“webr4”,如果使用 url localhost:8080/webr4/hello 它可以工作。现在我剩下的就是了解如何更改上下文(?),这样它就不会是 'webr4'。
  • 最简单的就是重命名WAR文件。
  • 如果这有助于您解决问题,请考虑 accepting this answer 以帮助其他用户。
【解决方案2】:

如果您在 eclipse:rest 中调用了您的项目,那么您应该会收到正确的消息:“Hello world!”从带有url的tomcat:

http://localhost:8080/rest/hello.

例如,如果您调用了您的项目:MyHello,那么使用您发布的代码,正确的 url 应该是:

http://localhost:8080/MyHello/hello

(原因是Eclipse默认创建一个war作为项目的名称,并将其发布到你在配置中添加的tomcat服务器)

告诉我

【讨论】:

  • 谢谢。确实这就是问题所在。正如我在上面的答案中评论的那样,它在更改时效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多