gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:34:46 25 4
gpt4 key购买 nike

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

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

enter image description here

这就是我的 POM 和 Web 的样子。我尝试将所需的 war 文件复制到 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>

最佳答案

异常(exception)情况下,它看起来像 com.sun.jersey.spi.container.servlet.ServletContainer类在您的类路径中不可用。

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

  • Jersey 1.x:com.sun.jersey
  • 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-server artifactId .

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-servlet artifactId .

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>

关于eclipse - 严重 : Servlet/JAXRS-HelloWorld threw load() exception java. lang.ClassNotFoundException : com. sun.jersey.spi.container.servlet.ServletContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com