java 在 Tomcat 7 中访问 Jersey 应用程序时出现 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16845581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:08:44  来源:igfitidea点击:

404 when accessing Jersey app in Tomcat 7

javaresttomcatjersey

提问by aviad cohen

I am a newby in web development and I am struggling to create a simple rest web service using jersey, packed as an independent war file, to be deployed on tomcat 7.

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

I have followed this tutorialin order to create simple hello world restful web service. I have used intelij in order to create the war file (both war file and exploded version of it) and deployed them on the tomcat\webapps folder.

我遵循本教程是为了创建简单的 hello world 宁静 Web 服务。我使用 intelij 来创建War文件(War文件和它的分解版本)并将它们部署在 tomcat\webapps 文件夹中。

When starting up the tomcat, I fail to access the url (http://localhost:8080/rest/hello) - getting 404.

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

Looking at the tomcat's logs, I can't find any exception or error, other then these next set of lines:

查看 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.

I have searched it a lot and tried to follow all solutions for this error but nothing worked.

我已经对其进行了大量搜索并尝试遵循此错误的所有解决方案,但没有任何效果。

I have the feeling that I missing something very basic, due to my newbi'ness. Any ideas what to search ? what am I missing ? it's a simple hello world app and I am exahuster and frustrated...

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

Java class

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

 <?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>

回答by Marcel St?r

This is similarto this SO question: "No provider classes found: when running Jersey REST example application".

类似于此 SO 问题:“未找到提供程序类:运行 Jersey REST 示例应用程序时”。

The message "No provider classes found" is fine, no need to worry about it. For the URL http://localhost:8080/rest/helloto work your WAR file needs to be called rest.war. Then restbecomes the context root. All the paths you define in the project's annotations are relative to the context root.

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

回答by Paolof76

if you have called your project in eclipse: rest, so you should get the correct message: "Hello world!" from tomcat with the url:

如果您在 eclipse 中调用了您的项目:rest,那么您应该得到正确的消息:“Hello world!” 来自 tomcat 的网址:

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

If you for example have called your project: MyHello, with the code you posted the correct url should be:

例如,如果您调用了您的项目:MyHello,则使用您发布的正确网址的代码应该是:

http://localhost:8080/MyHello/hello

(The reason is that Eclipse create by default a war called as the name of the project and publish it to the tomcat server you have added in your configuration)

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

Let me know

让我知道