【问题标题】:SEVERE: Servlet /JAXRS-HelloWorld threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer严重:Servlet /JAXRS-HelloWorld 抛出 load() 异常 java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
【发布时间】:2015-11-09 12:17:39
【问题描述】:

我阅读了大量与此问题相关的帖子,但没有得到任何帮助。

错误 感谢是否有人可以帮助我。

enter image description here

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

网络 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>

【问题讨论】:

  • 图像是一样的...无论如何,不​​要发布错误或代码的图像。编辑您的问题,将错误消息和代码作为格式正确的文本添加。
  • 我可以在图像中看到您使用的是 Apache Tomcat 7.0.x。我还可以看到您有一个 web.xml 将 Servlet API 声明为 3.1。请注意,Tomcat 7.0.x 支持 Servlet API 3.0。更多信息在Apache Tomcat documentation
  • 看看我更新的答案。

标签: eclipse rest tomcat jersey restful-url


【解决方案1】:

除此之外,您的类路径中似乎没有 com.sun.jersey.spi.container.servlet.ServletContainer 类。

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

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

所以,我了解到您使用的是 Jersey 1.x

Jersey 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的最新版本。

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

【讨论】:

  • 感谢您的回复,我检查了它并将服务器模板更改为 3.0,因为我正在使用 apache tomcat。尽管如此,我仍然得到同样的错误。请建议下一步可以做什么。
  • @user1007451 我希望我有一个水晶球来了解您的pom.xml 的样子。但我没有。因此,请编辑您的问题并发布您的pom.xml。你附上的图片都是一样的。
  • @user1007451 我的建议是:删除所有 Jersey 1.x 依赖项并添加 Jersey 2.x 依赖项。
  • 感谢您的回复,我也添加了 POM.XML
【解决方案2】:

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

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

这应该可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2017-10-20
    • 2014-01-08
    • 2014-12-03
    • 2014-09-16
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多