0

在我的应用程序中,我们被迫使用 tomcat 8.5,因为我们必须支持 servlet api 3.1,但我们升级到 spring boot 2.2.6。现在开始使用嵌入式tomcat时问题正在发生。如果我在我的 pom 中注释掉 tomcat 版本(获取 Tomcat 9),那么相同的代码就像一个魅力。

这是我的 EmbededTomcat 代码:

@Value("${spring.datasource.url}")
private String jdbcUrl;

@Value("${spring.datasource.username}")
private String jdbcUsername;

@Value("${spring.datasource.password}")
private String jdbcPassword;

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Bean
public TomcatServletWebServerFactory tomcatFactory() {
    log.info("initializing tomcat factory... ");        
    return new TomcatServletWebServerFactory() {

        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatWebServer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {    
            log.info("initializing tomcat factory JDNI ... ");
            // Adding connection details
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/DB");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", driverClassName);
            resource.setProperty("url", jdbcUrl);
            resource.setProperty("username", jdbcUsername);
            resource.setProperty("password", jdbcPassword);

            context.getNamingResources().addResource(resource);
        }
    };
}

在我的 pom.xml 上,我所做的唯一更改是:

<servlet-api.version>3.1.0</servlet-api.version> 
<!-- Forced for embeded tomcats since tomcat 9 force servlet 4.0 -->
<tomcat.version>8.5.54</tomcat.version>

这是完整的堆栈错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:175)

The following method did not exist:

    org.apache.tomcat.util.modeler.Registry.disableRegistry()V

The method's class, org.apache.tomcat.util.modeler.Registry, is available from the following locations:

    jar:file:/C:/Users/localAdministrator/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.54/tomcat-embed-core-8.5.54.jar!/org/apache/tomcat/util/modeler/Registry.class

It was loaded from the following location:

    file:/C:/Users/localAdministrator/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.54/tomcat-embed-core-8.5.54.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.apache.tomcat.util.modeler.Registry

可能的相关问题:

我正在尝试一个 Spring Boot 示例,但它显示以下错误.. 我该怎么办?

4

3 回答 3

0

这是一个非常丑陋的解决方案,可能不起作用,但你可以给它一个机会。

  1. 在您的应用程序包结构中创建,如 apache 的 lib /java/src/main/org/apache/tomcat/util/modeler/
  2. 将 Tomcat 8 中的 Registry 类放入此包中。
  3. 添加存根方法disableRegistry,例如

    public static synchronized void disableRegistry() {
        return new NoDescriptorRegistry();
    }
    
于 2020-04-14T11:03:20.670 回答
0

Spring Boot 2.2.6 至少需要 tomcat 9.0.x。参考 - https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/2.2.6.RELEASE

为了继续使用 spring 2.2.6 并带来 tomcat 8.5.x,也许您可​​以覆盖“spring-boot-starter-tomcat”工件(来自 spring-boot-parent:2.2.6)以获得兼容和所需的 tomcat 版本。

即 spring-boot-starter-tomcat:2.0.7.RELEASE 可以很好地与 tomcat 8.5.x 配合使用

在 pom 文件中声明如下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>2.0.7.RELEASE</version>
</dependency>
于 2020-11-10T17:50:02.173 回答
0

这是对我有用的 pom.xml 文件:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.9.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring.boot.version>2.0.9.RELEASE</spring.boot.version>
</properties>



<dependencies>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <!--<exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>-->
    </dependency>
    
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.0.7.RELEASE</version>
    </dependency>
    

    <!-- Include Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>-->
    </dependencies>
</dependencyManagement>


...
于 2021-05-02T00:18:39.257 回答