无法将 WAR 文件部署到 WildFly 28.0.1.Final - 无法启动服务 jboss.deployment.unit

Unable to deploy WAR file to WildFly 28.0.1.Final - Failed to start service jboss.deployment.unit

提问人:Vinícius do Carmo Melício 提问时间:7/4/2023 最后编辑:Vinícius do Carmo Melício 更新时间:7/6/2023 访问量:481

问:

我目前正在学习 Java,我正在尝试将一个简单的项目部署到 WildFly 28.0.1.Final,但我遇到了一个问题。该项目仅包含三个类(、、),并使用MySQL 8.0.33。我正在使用 Java 11 编写代码。HelloServletSentenceSentenceRepository

就我到目前为止所尝试的内容而言,我尝试在文件中调整我的项目的依赖关系,认为可能存在与库兼容性相关的问题或缺少某些内容。我也尝试调整和配置,虽然我不完全确定我需要更改什么,但我想看看那里的修改是否能解决问题。pom.xmlbeans.xmlpersistence.xml

至于我的期望,当我将我的 WAR 文件部署到 WildFly 时,我希望服务器能够顺利启动。我以为它会读取 、 并正确地连接所有依赖项,初始化我的 HelloServlet,并准备好处理传入的请求。我很惊讶,因为我检查了我的代码,并且没有明显的地方可以访问应该为 null 的东西。pom.xmlbeans.xmlpersistence.xmlNullPointerException

从本质上讲,我的目标是了解如何使用 WildFly、MySQL 和 Java 设置一个简单的项目,并让这个项目顺利运行。如果能提供任何关于查明其根本原因并加以解决的指导意见,将不胜感激。NullPointerException

我的项目结构:

├── pom.xml
├── README.md
└── src
    └── main
        ├── java
        │   └── com
        │       └── penguin
        │           ├── model
        │           │   └── Sentence.java
        │           ├── repository
        │           │   └── SentenceRepository.java
        │           └── servlet
        │               └── HelloServlet.java
        ├── resources
        │   └── META-INF
        │       ├── beans.xml
        │       └── persistence.xml
        └── webapp
            ├── index.jsp
            └── WEB-INF
                └── web.xml

每当我尝试部署 WAR 文件时,都会收到以下错误:

22:26:49,900 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 144) MSC000001: Failed to start service jboss.deployment.unit."language-study.war".undertow-deployment: org.jboss.msc.service.StartException in service jboss.deployment.unit."language-study.war".undertow-deployment: java.lang.RuntimeException: java.lang.NullPointerException
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:90)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at or[email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:829)
    at [email protected]//org.jboss.threads.JBossThread.run(JBossThread.java:513)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at [email protected]//io.undertow.servlet.core.DeploymentManagerImpl.undeploy(DeploymentManagerImpl.java:686)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:110)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:87)
    ... 8 more
Caused by: java.lang.NullPointerException
    at [email protected]//io.undertow.servlet.core.DeploymentImpl.createThreadSetupAction(DeploymentImpl.java:176)
    at [email protected]//io.undertow.servlet.core.DeploymentManagerImpl.undeploy(DeploymentManagerImpl.java:669)
    ... 10 more

22:26:49,908 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "language-study.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"language-study.war\".undertow-deployment" => "java.lang.RuntimeException: java.lang.NullPointerException
    Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    Caused by: java.lang.NullPointerException"}}
22:26:49,909 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "language-study.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"language-study.war\".undertow-deployment" => "java.lang.RuntimeException: java.lang.NullPointerException
    Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    Caused by: java.lang.NullPointerException"}}
22:26:49,909 ERROR [org.jboss.as.server] (External Management Request Threads -- 1) WFLYSRV0021: Deploy of deployment "language-study.war" was rolled back with the following failure message: 
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"language-study.war\".undertow-deployment" => "java.lang.RuntimeException: java.lang.NullPointerException
    Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    Caused by: java.lang.NullPointerException"}}

我不确定这是从哪里来的。我无法在我的代码或服务器配置中找到问题的根源NullPointerException

我:persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="LanguageStudyDS" transaction-type="JTA">
        <jta-data-source>java:/LanguageStudyDS</jta-data-source>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

我:beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
      http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

我:web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.penguin.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

我: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.penguin</groupId>
  <artifactId>language-study</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>language-study Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.release>11</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>jakarta.platform</groupId>
      <artifactId>jakarta.jakartaee-api</artifactId>
      <version>10.0.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.0.33</version>
    </dependency>
    <dependency>
      <groupId>jakarta.persistence</groupId>
      <artifactId>jakarta.persistence-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.orm</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>6.2.6.Final</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.wildfly</groupId>
      <artifactId>wildfly-jpa</artifactId>
      <version>28.0.1.Final</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.faces</groupId>
      <artifactId>javax.faces-api</artifactId>
      <version>2.3</version> 
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>2.4.8.Final</version> 
    </dependency>
  </dependencies>
  <build>
    <finalName>language-study</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

我:HelloServlet.java

public class HelloServlet extends HttpServlet {
    
    @EJB
    private SentenceRepository repo;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        if (repo == null) {
            out.println("<h1>Error: repo object is null</h1>");
            return;
        }

        Sentence sentence = null;
        try {
            // Inserting a sentence
            sentence = new Sentence("你好,世界");
            repo.save(sentence);
        } catch (Exception ex) {
            ex.printStackTrace();
            out.println("<h1>Error: " + ex.getMessage() + "</h1>");
        }

        try {
            // Fetching a sentence
            Sentence fetchedSentence = repo.find(sentence.getId());
            out.println("<h1>Id: " + fetchedSentence.getId() + ", Sentence: " + fetchedSentence.getMandarinSentence() + "</h1>");
        } catch (Exception ex) {
            ex.printStackTrace();
            out.println("<h1>Error: " + ex.getMessage() + "</h1>");
        }
    }
}

我:SentenceRepository.java

@Stateless
public class SentenceRepository {

    @PersistenceContext(unitName = "LanguageStudyDS")
    private EntityManager entityManager;

    public void save(Sentence sentence) {
        try {
            entityManager.persist(sentence);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public Sentence find(Long id) {
        try {
            return entityManager.find(Sentence.class, id);
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    public void delete(Long id) {
        try {
            Sentence sentence = find(id);
            if (sentence != null && entityManager != null) {
                entityManager.remove(sentence);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void update(Long id, String newSentence) {
        try {
            Sentence sentence = find(id);
            if (sentence != null) {
                sentence.setMandarinSentence(newSentence);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

我:Sentence.java

@Entity
@Table(name = "sentences")
public class Sentence {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "mandarin_sentence")
    private String mandarinSentence;

    public Sentence() {
    }

    public Sentence(String mandarinSentence) {
        this.mandarinSentence = mandarinSentence;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMandarinSentence() {
        return mandarinSentence;
    }

    public void setMandarinSentence(String mandarinSentence) {
        this.mandarinSentence = mandarinSentence;
    }

}

我的(在 WildFly 内部):module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-j-8.0.33.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

我对(也在 WildFly 内部)所做的修改:standalone.xml

<datasource jndi-name="java:/LanguageStudyDS" pool-name="LanguageStudyDS" enabled="true" use-java-context="true">
        <connection-url>jdbc:mysql://localhost:3306/language_study?useSSL=false</connection-url>
        <driver>mysql</driver>
        <security>
            <user-name>username</user-name>
            <password>password</password>
        </security>
</datasource>
<driver name="mysql" module="com.mysql">
    <driver-class>com.mysql.cj.jdbc.Driver</driver-class>
</driver>
java mysql 部署 nullpointerexception wildfly

评论

0赞 JCompetence 7/4/2023
这里有一个混淆..Wilfly 不会下载您的依赖项。您应该使用 Maven 进行全新安装,它将为您创建一个包含所有依赖项的 WAR 文件。I was expecting the server to start without issue. I thought it would read the pom.xml, beans.xml, and persistence.xml correctly, wire up all dependencies,
0赞 Vinícius do Carmo Melício 7/4/2023
@JCompetence 感谢您的回复。也许我应该澄清一下 - 我已经用来构建我的项目和正在部署的 WAR 文件。问题不在于缺少依赖项,因为构建在没有任何.提到的文件是在服务器能够正确读取和解释这些配置文件的上下文中,而不是关于获取依赖项。我的问题与尝试部署此 WAR 时 WildFly 上的服务启动问题有关。你对这个特定问题有什么见解吗?mvn clean installClassNotFoundException

答:

1赞 ehsavoie 7/4/2023 #1

1- 您提供带有依赖项的 WAR,WildFly 将提供 JakartEE 10 API 实现。 2- 应该提供您的驱动程序,因为您正在创建一个模块来提供它。 3- 您的 XML 描述符不是 JakartaEE 10,而是 Java EE 4- 错误发生在取消部署期间,而不是在部署本身期间。也许之前有错误 5- 您应该查看 WildFly Quickstarts https://github.com/wildfly/quickstart/tree/28.0.1.Final(todo-backend 可以显示您在此处尝试实现的目标)

评论

0赞 Vinícius do Carmo Melício 7/4/2023
感谢您的建议。我已将mysql-connector-j更新为“provided”,并尝试将XML描述符调整为JakartaEE 10,但问题仍然存在。我热衷于了解符合 JakartaEE 10 标准的 XML 描述符(web.xml、持久性 .xml、bean.xml)的最佳配置。WildFly 快速入门存储库一直是一个宝贵的资源。我相信我可能在这里遗漏了一些微不足道的东西,并希望有任何其他可能指导我朝着正确方向前进的指示或例子。再次感谢您的帮助。
0赞 James R. Perkins 7/5/2023
需要升级到雅加达版本并标记为提供。这是 Jakarta EE 8 版本,不能与 Jakarta EE 10 一起使用。javax.faces:javax.faces-api
0赞 Vinícius do Carmo Melício 7/6/2023 #2

在为这个问题挣扎了一段时间后,我终于能够找到解决方案。这是我解决它的方法:

我不得不调整我的文件以与 Jakarta EE 10 兼容:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
     version="6.0">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.penguin.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

然后我不得不将 from 移动到 .beans.xmlsrc/main/resources/META-INFsrc/main/webapp/WEB-INF

我旅程的最后一部分是处理项目的依赖项。我必须确保它们能够与 Jakarta EE 10 和 WildFly 28.0.1.Final 一起使用。我仔细查看了,进行了必要的调整,并设置了尽可能多的依赖项。这是我最终依赖项的样子:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.13.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>jakarta.platform</groupId>
  <artifactId>jakarta.jakartaee-api</artifactId>
  <version>10.0.0</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jakarta.servlet</groupId>
  <artifactId>jakarta.servlet-api</artifactId>
  <version>6.0.0</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
  <version>8.0.33</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jakarta.persistence</groupId>
  <artifactId>jakarta.persistence-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.wildfly</groupId>
  <artifactId>wildfly-jpa</artifactId>
  <version>28.0.1.Final</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jakarta.faces</groupId>
  <artifactId>jakarta.faces-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jakarta.enterprise</groupId>
  <artifactId>jakarta.enterprise.cdi-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>

我要感谢在此故障排除过程中提供帮助的每个人。我现在可以毫无问题地部署和运行我的代码。我希望这个解决方案对遇到类似问题的任何其他人都有帮助。