【问题标题】:"Could not initialize class..." error in pom.xml with a new Maven project新 Maven 项目的 pom.xml 中出现“无法初始化类...”错误
【发布时间】:2021-09-09 06:47:19
【问题描述】:

我想了解如何使用 JAX-RS 开发 RESTful API,因此我从一个 YouTube 视频中介绍的示例开始。该视频创建于 5 -6 年前,使用 Tomcat 7 和旧版本的 Eclipse。但我安装了最新版本的 Tomcat (10.0.6) 和 Eclipse (jee-2021-03) 并按照教程进行操作。

我使用以下原型创建了一个新的 maven 项目: 组 ID:org.glassfish.jersey.archetypes 工件 ID:jersey-quickstart-webapp 版本:3.0.2

我为我的项目提供了默认版本 0.0.1-SNAPSHOT 的 Group Id 和 Artifact Id,然后单击 Finish。

Eclipse 创建项目时出现以下 2 个错误:

  1. index.jsp:“在 Java 构建路径中找不到超类“javax.servlet.http.HttpServlet””
  2. pom.xml:“无法初始化类 org.apache.maven.plugin.war.util.WebappStructureSerializer”在第一行。

如果有人能帮助我了解为什么会出现这些错误以及如何解决这些错误,我将不胜感激。

这是我的 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.xyz.rest</groupId>
    <artifactId>onlineshopping</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlineshopping</name>

    <build>
        <finalName>onlineshopping</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>3.0.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

【问题讨论】:

  • 这有点难说。也许您应该从不同的教程开始:howtodoinjava.com/spring-boot/spring-boot-jersey-example
  • 感谢 David 推荐另一个教程。但我想完成我的教程,因为我看了几部同一个人的理论视频,我喜欢他的系列。但是看到这个错误令人沮丧。您能否建议一些我可以尝试的事情,或者董事会上可以提供帮助的人?

标签: java eclipse maven


【解决方案1】:

第一个问题:

  1. 右键单击您的项目
  2. 点击属性
  3. 单击目标运行时
  4. 勾选 Apache Tomcat v10.0
  5. 点击应用并关闭

第二个问题: 在 build -> plugins

的 pom.xml 文件中添加以下插件
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
</plugin>

你的 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.xyz.rest</groupId>
    <artifactId>onlineshopping</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlineshopping</name>

    <build>
        <finalName>onlineshopping</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>3.0.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    对于 1/ index.jsp 在 pom.xml 中添加如下依赖

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    

    对于 2/ pom.xml 在 eclipse.ini 末尾添加以下属性并重启 eclipse

    --illegal-access=permit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-20
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      相关资源
      最近更新 更多