我的球衣服务应用程序显示 Class 未找到异常。但是我已经配置了对pom文件的依赖

My jersey service application shows Class not found exception. But i have configured these dependancy in my pom file

Error like this:- java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)

当我把所有的 jar 文件都放在 lib (WEB-INF/lib) 下时它会起作用 folder.But 我想要 pom 文件来解决这个问题。

这完全取决于您的配置。 pom.xml 和不完整的教程。如果您希望 pom.xml 为您解决这个问题,您需要添加一些内容。

插件

要事第一。小红叉和事实,即您的项目在 Java 1.5 下配置为 运行 让我猜测,您的部门存在兼容性问题。特别是 javax.ws.rs \ javax.ws.rs-api.

要解决这个问题,您可能希望 maven 通过 maven-compiler-plugin:

处理它
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <inherited>true</inherited>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

然后,您可能想要 运行 服务器 "in" eclipse。这里可以使用tomcat7-maven-plugin

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <port>8080</port>
        <path>/</path>
    </configuration>
</plugin>

最后使用的插件是 maven-war-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>

泽西属地

要开始使用 Jersey,您只需配置一个依赖:

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

我需要说的是,有 newer versions available,但您可能有理由使用 1.19

您完整的 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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>sujith</groupId>
    <artifactId>jersey-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.maven-compiler-plugin.version>3.5.1</project.maven-compiler-plugin.version>
        <project.tomcat7-maven-plugin.version>2.2</project.tomcat7-maven-plugin.version>
        <project.maven-war-plugin.version>2.6</project.maven-war-plugin.version>
    </properties>

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

    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <outputDirectory>${project.artifactId}</outputDirectory>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${project.maven-compiler-plugin.version}</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>${project.tomcat7-maven-plugin.version}</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>${project.maven-war-plugin.version}</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>

在 Eclipse 中,您现在右键单击您的项目,然后 select -> 运行 As / Debug As -> Maven build...

编辑配置并启动菜单中,您现在添加目标:clean install tomcat7:run-war就是这样。

您的服务器应该启动并且资源在
http://127.0.0.1:8080 / {web.xml\servlet-mapping\url-pattern} / {path-to-resource}

下可用

最后一件事。请先查看原始示例。大多数教程都是废话。最后,请阅读 how to ask 页。

祝你有愉快的一天。