java.lang.NoClassDefFoundError when deploying Spring Boot .war within .ear on Wildfly with other deployments

huangapple go评论67阅读模式
英文:

java.lang.NoClassDefFoundError when deploying Spring Boot .war within .ear on Wildfly with other deployments

问题

我在Wildfly上部署Spring Boot应用程序时遇到了java.lang.NoClassDefFoundError错误,该应用程序以.war文件形式打包在一个.ear部署包中。只有在.ear文件中包含其他部署时才会出现此错误。

然而,如果我将.war文件部署在一个没有其他部署的新的Wildfly实例上,就不会遇到任何问题。

详细信息:

这是我看到的具体错误消息:

ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 142) MSC000001: Failed to start service jboss.deployment.subunit."interfaces2.ear"."redacted-kafka-consumer.war".undertow-deployment: 
org.jboss.msc.service.StartException in service jboss.deployment.subunit."interfaces2.ear"."redacted-kafka-consumer.war".undertow-deployment: java.lang.RuntimeException: java.lang.IllegalStateException: Error processing con
dition on org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.basicErrorController
    ...

这个错误似乎与Spring Framework的spring-boot-web模块中的DefaultErrorAttributes类有关。看起来类加载器无法找到必要的HandlerExceptionResolver类,导致运行时的NoClassDefFoundError错误。

附加信息:

  • Spring Boot 版本: 2.7.11
  • Wildfly 版本: 23
  • Java 版本: 11

这是redacted-kafka-consumer模块的pom.xml文件中相关的依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>${servlet-api.version}</version>
    <scope>provided</scope>
</dependency>

非常感谢您提供帮助或解决此问题的建议。谢谢!

英文:

I'm encountering a java.lang.NoClassDefFoundError when deploying a Spring Boot application as a .war file within a .ear deployment on Wildfly. The error only occurs when there are other deployments included in the .ear file.

However, if I deploy the .war file on a fresh Wildfly instance with no other deployments, I don't encounter any issues.

Details:

Here's the specific error message I'm seeing:

ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 142) MSC000001: Failed to start service jboss.deployment.subunit.&quot;interfaces2.ear&quot;.&quot;redacted-kafka-consumer.war&quot;.undertow-deployment: 
org.jboss.msc.service.StartException in service jboss.deployment.subunit.&quot;interfaces2.ear&quot;.&quot;redacted-kafka-consumer.war&quot;.undertow-deployment: java.lang.RuntimeException: java.lang.IllegalStateException: Error processing con
dition on org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.basicErrorController
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:81)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1348)
at java.base/java.lang.Thread.run(Thread.java:834)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.JBossThread.run(JBossThread.java:513)
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.basicErrorController
at io.undertow.servlet@2.2.5.Final//io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:257)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:96)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:78)
... 8 more
Caused by: java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.basicErrorController
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60)
at deployment.interfaces2.ear//org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108)
at deployment.interfaces2.ear//org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:193)
at deployment.interfaces2.ear//org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:153)
at deployment.interfaces2.ear//org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:129)
at deployment.interfaces2.ear//org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:343)
at deployment.interfaces2.ear//org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247)
at deployment.interfaces2.ear//org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311)
at deployment.interfaces2.ear//org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112)
at deployment.interfaces2.ear//org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:748)
at deployment.interfaces2.ear//org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
at deployment.interfaces2.ear//org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
at deployment.interfaces2.ear//org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
at deployment.interfaces2.ear//org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at deployment.interfaces2.ear//org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at deployment.interfaces2.ear//org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:175)
at deployment.interfaces2.ear//org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:155)
at deployment.interfaces2.ear//org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:97)
at deployment.interfaces2.ear//org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:174)
at io.undertow.servlet@2.2.5.Final//io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:204)
at io.undertow.servlet@2.2.5.Final//io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:187)
at io.undertow.servlet@2.2.5.Final//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
at org.wildfly.extension.undertow@23.0.2.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
at io.undertow.servlet@2.2.5.Final//io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:255)
... 10 more
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration] from ClassLoader [ModuleClassLoader for Module &quot;deployment.interfaces2.ear.redacted-kafka-consumer.war&quot; from Service Module Loader]
at deployment.interfaces2.ear//org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:485)
at deployment.interfaces2.ear//org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:361)
at deployment.interfaces2.ear//org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:418)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:765)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:764)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:703)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:674)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1684)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:570)
at deployment.interfaces2.ear//org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:542)
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:246)
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:239)
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:229)
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:182)
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:157)
at deployment.interfaces2.ear.redacted-kafka-consumer.war//org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
... 38 more
Caused by: java.lang.NoClassDefFoundError: Failed to link org/springframework/boot/web/servlet/error/DefaultErrorAttributes (Module &quot;deployment.interfaces2.ear&quot; from Service Module Loader): org/springframework/web/servlet/HandlerExceptionResolver
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1095)
at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:424)
at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:555)
at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:339)
at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:126)
at org.jboss.modules.Module.loadModuleClass(Module.java:753)
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:247)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167)
at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310)
at deployment.interfaces2.ear//org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467)
... 54 more

This error seems to be related to the class DefaultErrorAttributes from Spring Framework's spring-boot-web module. It appears that the class loader is unable to find the necessary class HandlerExceptionResolver, leading to the NoClassDefFoundError at runtime.

Additional Information:

  • Spring Boot version: 2.7.11
  • Wildfly version: 23
  • Java version: 11

Here are the relevant dependencies from the pom.xml file of the redacted-kafka-consumer module:

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
					&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
			&lt;scope&gt;provided&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
			&lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
			&lt;version&gt;${servlet-api.version}&lt;/version&gt;
			&lt;scope&gt;provided&lt;/scope&gt;
		&lt;/dependency&gt;

Any help or suggestions on how to resolve this issue would be greatly appreciated. Thank you!

答案1

得分: 1

"[加载DispatcherServlet的默认策略类错误](https://stackoverflow.com/a/20267074/6309)" 或 "[请告诉我Spring MVC错误](https://stackoverflow.com/a/45453371/6309)" 都指向模块之间的某个版本不一致。

因此,在构建war时,请仔细检查pom.xml加载的当前版本:

``` bash
mvn versions:display-dependency-updates

并检查是否有任何您可能考虑升级的更新:

mvn versions:display-dependency-updates

检查是否会在构建可交付物时,使用DependencyConvergence规则来执行 enforcer 失败:

&lt;project&gt;
  [...]
  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.3.0&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;enforce&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;enforce&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;rules&gt;
                &lt;DependencyConvergence /&gt;
              &lt;/rules&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
  [...]
&lt;/project&gt;

<details>
<summary>英文:</summary>
&quot;[Error loading DispatcherServlet&#39;s default strategy class](https://stackoverflow.com/a/20267074/6309)&quot; or &quot;[Please advise to me that Spring MVC Error](https://stackoverflow.com/a/45453371/6309)&quot; are both pointing to some version discrepancy between modules.
So double-check the current versions loaded by your pom.xml when building your war:
``` bash
mvn versions:display-dependency-updates

And check if there is any update you might want to consider upgrading to:

mvn versions:display-dependency-updates

Check if an enforcer would fail your build, using a DependencyConvergence rule, when building your deliverable:

&lt;project&gt;
  [...]
  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.3.0&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;enforce&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;enforce&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;rules&gt;
                &lt;DependencyConvergence /&gt;
              &lt;/rules&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
  [...]
&lt;/project&gt;

huangapple
  • 本文由 发表于 2023年7月10日 19:35:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定