严重:Servlet /JAXRS-HelloWorld 抛出 load() 异常 java.lang.ClassNotFoundException:com.sun.jersey.spi.container.servlet.ServletContainer

SEVERE: Servlet /JAXRS-HelloWorld threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

我阅读了大量与此问题相关的帖子,但无法获得任何帮助。

错误 如果有人能帮助我,我将不胜感激。

enter image description here

这就是我的 POM 和 Web 的样子。我尝试将所需的 war 文件复制到 C:\appache tomcat 7 但没有成功。即使在 eclipse 中部署它也会给我同样的错误。

WEb enter image description here

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.tutorialacademy.rest</groupId>
  <artifactId>helloworld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>helloworld Maven Webapp</name>
  <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>  
 <repository>  
  <id>maven2-repository.java.net</id>  
  <name>Java.net Repository for Maven</name>  
  <url>http://download.java.net/maven/2/</url>  
  <layout>default</layout>  
 </repository>  
</repositories>  

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
     <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-apache-client</artifactId>
    <version>1.19</version>
</dependency>
    </dependencies>



    <build>
        <sourceDirectory>src/main/java</sourceDirectory>

        <plugins>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
               </configuration>
          </plugin>


        </plugins>

    </build>
</project>

异常情况下,com.sun.jersey.spi.container.servlet.ServletContainer class 在您的 class 路径中似乎不可用。

请注意 Jersey 1.xJersey 2.x 使用不同的包名称:

  • 球衣 1.x: com.sun.jersey
  • 球衣 2.x: org.glassfish.jersey

所以,我了解到您正在使用 泽西 1.x

泽西岛1.x依赖性

要使用 Jersey 1.x,您的 pom.xml 中需要以下依赖项:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.19</version>
</dependency>

要阅读更多关于 Jersey 1.x 依赖项的信息,请查看 documentation

您可以在Maven Repository.

中查看jersey-serverartifactId的最新版本

泽西岛2.x依赖性

如果您想使用 Jersey 2.x,您必须将以下依赖项添加到您的 pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <!-- if your container implements Servlet API older than 3.0, 
         use "jersey-container-servlet-core"  -->
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.1</version>
</dependency>

您可以在Maven Repository.

中查看jersey-container-servletartifactId的最新版本

documentation.

中阅读更多关于 Jersey 2.x 依赖项的信息

更新

如果可以,我建议使用 Jersey 2.x 而不是 Jersey 1.x

为此,请使用以下 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.tutorialacademy.rest</groupId>
    <artifactId>helloworld</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Hello World</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>  
        <repository>  
            <id>maven2-repository.java.net</id>  
            <name>Java.net Repository for Maven</name>  
            <url>http://download.java.net/maven/2/</url>  
            <layout>default</layout>  
        </repository>  
    </repositories>  

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- if your container implements Servlet API older than 3.0, 
                 use "jersey-container-servlet-core"  -->
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.1</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

以及以下 web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

</web-app>

如果您尝试使用 Tomcat 和 Eclipse 中的 Jersey 库创建 Web 应用程序以提供 Web 服务,但在类路径中添加 jersey-server.jar 后仍然遇到问题。您可以按照以下步骤添加对部署程序集的依赖。

项目 -> 属性 -> 开发程序集 -> 添加 -> java 构建路径条目 -> maven 依赖项

这应该有效。