【问题标题】:Tomcat error 404: The origin server did not find a current representation for the target resource or is not willing to disclose that one existsTomcat 错误 404:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示
【发布时间】:2020-02-27 20:16:35
【问题描述】:

我正在尝试使用 apache Tomcat 部署一个简单的 Jax-RS 应用程序。服务器在我的本地主机上正常运行,tomcat 主页已启动。但是当我在我的服务器上从 eclipse 运行时,我得到一个状态 404 错误。有谁知道这是为什么?

Pom.xml


    project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.javaee8</groupId>
      <artifactId>hello-javaee8</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>hello-javaee8 Maven Webapp</name>
      <url>http://maven.apache.org</url>


      <dependencies>
      <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>8.0</version>
      <scope>provided</scope>
      </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>

        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

      </dependencies>
      <build>
        <finalName>hello-javaee8</finalName>
      </build>
    </project>

Web.xml


    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    </web-app>

如果我需要添加任何其他文件以找到此解决方案,请告诉我

【问题讨论】:

  • [1] 更新您的问题以指定您从浏览器提交的内容以获得 404。[2] 提供 pom.xml 的完整列表。您当前显示的内容无效。
  • 我的解决方案对您有用吗?如果是,请选择绿色复选标记

标签: tomcat jakarta-ee jax-rs


【解决方案1】:

有两个原因。

首先,您不包含 JAX-RS 实现,仅包含 JAX-RS API。 JAX-RS API 仅提供一组接口,并由实现来提供这些接口的具体实现。

如果您没有 JAX-RS 实现,我建议您使用 Jersey(参考实现)。将以下内容添加到您的pom.xml

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.29</version>
</dependency>

其次,您还没有在web.xml 的任何地方注册您的应用程序。如何执行此操作取决于您使用的 JAX-RS 实现以及如何注册您的类和提供程序。为了帮助您解决这个问题,我需要您回答几个问题:

  1. 您有实现Application 的类(或扩展ResourceConfig 的类)吗?
  2. 您的所有资源类是否都使用@Path 注释,您所有的提供程序是否都使用@Provider 注释?
  3. 您的所有资源类和提供程序都在同一个包中吗?
  4. 您打算使用哪种 JAX-RS 实现?

如果您使用 Jersey 并且对第一个问题的回答是肯定的,请将以下内容放入 web.xml

<servlet>
    <servlet-name>Jersey Web Application</filter-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value><!-- FQN of your Application/ResourceConfig class --></param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

否则,请查看特定 JAX-RS 框架的设置说明。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2019-06-13
    相关资源
    最近更新 更多