【问题标题】:Getting java.lang.NoClassDefFoundError: org/apache/catalina/connector/Connector in wildFly 21.x获取 java.lang.NoClassDefFoundError: org/apache/catalina/connector/Connector in wildFly 21.x
【发布时间】:2021-09-12 21:25:09
【问题描述】:

我正在尝试运行一个在嵌入式 tomcat 上完美运行的 Spring Boot 项目,但是当部署到 Wildfly 21.x 时抛出 java.lang.NoClassDefFoundError: org/apache/catalina/connector/Connector

任何帮助将不胜感激。

下面是我的代码 sn-p。

主文件:-

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class CdPlaylistadapterApplication {

    @Value("${http.port}")
    private int httpPort;

    public static void main(String[] args) {
        SpringApplication.run(CdPlaylistadapterApplication.class, args);
    }

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }

    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
}

我的 pom.xml sn-p

        <cxf.version>3.4.0</cxf.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
             <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
             </exclusion>
         </exclusions>
        </dependency>
        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>javax.servlet-api</artifactId>
             <scope>provided</scope>
          </dependency> 
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

错误:-

Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.att.dtss.playlistadapter.CdPlaylistadapterApplication] from ClassLoader [ModuleClassLoader for Module "deployment.cd-playlistadapter-0.0.1-SNAPSHOT.war" from Service Module Loader]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:455)
        at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:151)
        ... 39 more
Caused by: java.lang.NoClassDefFoundError: org/apache/catalina/connector/Connector
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.getDeclaredMethods(Class.java:1975)
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
        ... 41 more
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.connector.Connector from [Module "deployment.cd-playlistadapter-0.0.1-SNAPSHOT.war" from Service Module Loader]
        at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:255)
        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)
        ... 45 more

【问题讨论】:

    标签: java spring-boot tomcat jboss wildfly


    【解决方案1】:

    org.apache.catalina.connector.Connector 类是 Tomcat 的一部分。为了让您的应用程序在外部 servlet 容器上正常工作,Tomcat 的嵌入式副本必须位于 Web 应用程序的类路径中(即在 /WEB-INF/lib 中)。您可以通过将spring-boot-starter-tomcat 的范围设置为provided 来正确设置它。

    但是现在你遇到了一个问题,因为 CdPlaylistadapterApplication 在其方法签名之一中有 Connector,并且 Spring Boot 在创建 CdPlaylistadapterApplication 类的实例时失败。

    要解决这个问题,您需要将 Tomcat 特定配置移动到另一个类(甚至是嵌套类)并使用 @ConditionalOnClass 注释保护它:

    @SpringBootApplication
    public class CdPlaylistadapterApplication extends SpringBootServletInitializer {
    
       @Configuration
       @ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
       static class EmbeddedConfiguration {
    
          @Value("${http.port}")
          private int httpPort;
    
          @Bean
          public ServletWebServerFactory servletContainer() {
             TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
             tomcat.addAdditionalTomcatConnectors(createStandardConnector());
             return tomcat;
          }
    
          private Connector createStandardConnector() {
             Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
             connector.setPort(httpPort);
             return connector;
          }
       }
    
       public static void main(String[] args) {
          SpringApplication.run(CdPlaylistadapterApplication.class, args);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2020-11-05
      • 2015-04-27
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2018-05-24
      相关资源
      最近更新 更多