"Could not initialize class..." pom 中的错误.xml与新的 Maven 项目



我想学习如何使用JAX-RS开发RESTful API,因此从YouTube视频中的一个例子开始。该视频创建于5-6年前,使用Tomcat7和Eclipse的旧版本。但我安装了Tomcat(10.0.6(和Eclipse(jee-2021-03(的最新版本,并遵循了教程。

我创建了一个新的maven项目,原型如下:组Id:org.lassfish.jersey.protypes工件Id:jersey quickstart webapp版本:3.0.2

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

Eclipse创建的项目有以下2个错误:

  1. index.jsp:"超类";javax.servlet.http.HttpServlet";在Java构建路径">
  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>

第一个问题:

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

第二个问题:在build内部的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>

希望这能解决你的问题。

对于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
  1. 无法初始化类org.apache.maven.plugin.war.util.WebappStructureSerializer

    此消息不是错误,而是eclipse新版本的错误,因此您需要使用旧版本或等待修复此错误。

  2. 为了解决这个问题,您可以在pom中使用以下代码。XML文件尝试这个它将修复错误消息-:

    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.3.1</version>
    </plugin>
    </plugins>
    

最新更新