Tomcat7 和Spring开机war启动

Tomcat 7 and Spring boot war start up

我将 war 部署到 Tomcat 7 时收到以下消息:

nested exception is java.lang.NoClassDefFoundError: javax/el/ELManager

我将 <tomcat.version>7.0.61</tomcat.version> 添加到我的 pom.xml,现在可以在我的开发环境中重现错误。

错误发生在创建 bean 之后,其他更改是我所做的 war 到 运行 所必需的。

我试图更改我的 pom.xml 以便它使用可能与 tomcat 7 兼容的旧版本,但未能解决问题。不知道是哪个包导致的问题。

参见pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxx</groupId>
    <artifactId>monitormonitor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>monitormonitor</name>
    <description>Web Service Monitoring project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <start-class>com.xxx.monitormonitor.MonitormonitorApplication</start-class>
        <!-- deploying war-file on tomcat 7 -->
        <tomcat.version>7.0.61</tomcat.version><!-- nested exception is java.lang.NoClassDefFoundError: javax/el/ELManager -->
        <servlet-api.version>3.0.1</servlet-api.version>
    </properties>

    <packaging>war</packaging>

    <dependencies>
        <!-- added other version of log4j for invalid byte tag error -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <!-- changed version vs. ClassFormatException: Invalid byte tag in constant pool: 19 -->
            <!--<version>2.11.1</version>-->
            <version>2.7</version>
            <!--<scope>provided</scope>-->
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- <scope>provided</scope> -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.1.6.RELEASE</version>            
            <scope>provided</scope>
        </dependency>
        -->
        <!-- added vs java.lang.NoClassDefFoundError: javax/el/ELManager -->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>    
                    <mainClass>com.xxx.monitormonitor.MonitormonitorApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

依赖树:

    [INFO] com.xxx:monitormonitor:war:0.0.1-SNAPSHOT
    [INFO] +- org.apache.logging.log4j:log4j-api:jar:2.7:compile
    [INFO] +- org.apache.logging.log4j:log4j-core:jar:2.7:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.1.2.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.1.2.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.2.RELEASE:compile
    [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
    [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
    [INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.1:compile
    [INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
    [INFO] |  |  \- org.yaml:snakeyaml:jar:1.23:runtime
    [INFO] |  +- com.zaxxer:HikariCP:jar:3.2.0:compile
    [INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
    [INFO] |  \- org.springframework:spring-jdbc:jar:5.1.4.RELEASE:compile
    [INFO] |     \- org.springframework:spring-tx:jar:5.1.4.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:2.1.2.RELEASE:compile
    [INFO] |  \- com.sun.mail:javax.mail:jar:1.6.2:compile
    [INFO] |     \- javax.activation:activation:jar:1.1:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.1.2.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.1.2.RELEASE:compile
    [INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile
    [INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
    [INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
    [INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile
    [INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile
    [INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile
    [INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.14.Final:compile
    [INFO] |  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
    [INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
    [INFO] |  |  \- com.fasterxml:classmate:jar:1.4.0:compile
    [INFO] |  +- org.springframework:spring-web:jar:5.1.4.RELEASE:compile
    [INFO] |  \- org.springframework:spring-webmvc:jar:5.1.4.RELEASE:compile
    [INFO] |     +- org.springframework:spring-aop:jar:5.1.4.RELEASE:compile
    [INFO] |     \- org.springframework:spring-expression:jar:5.1.4.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.2.RELEASE:provided
    [INFO] |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
    [INFO] |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:7.0.61:provided
    [INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:7.0.61:provided
    [INFO] |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:7.0.61:provided
    [INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.1.2.RELEASE:runtime
    [INFO] |  +- org.springframework.boot:spring-boot:jar:2.1.2.RELEASE:compile
    [INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.2.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.1.2.RELEASE:test
    [INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.1.2.RELEASE:test
    [INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.1.2.RELEASE:test
    [INFO] |  +- com.jayway.jsonpath:json-path:jar:2.4.0:test
    [INFO] |  |  \- net.minidev:json-smart:jar:2.3:test
    [INFO] |  |     \- net.minidev:accessors-smart:jar:1.2:test
    [INFO] |  |        \- org.ow2.asm:asm:jar:5.0.4:test
    [INFO] |  +- junit:junit:jar:4.12:test
    [INFO] |  +- org.assertj:assertj-core:jar:3.11.1:test
    [INFO] |  +- org.mockito:mockito-core:jar:2.23.4:test
    [INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.9.7:test
    [INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.9.7:test
    [INFO] |  |  \- org.objenesis:objenesis:jar:2.6:test
    [INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
    [INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.0:test
    [INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
    [INFO] |  +- org.springframework:spring-core:jar:5.1.4.RELEASE:compile
    [INFO] |  |  \- org.springframework:spring-jcl:jar:5.1.4.RELEASE:compile
    [INFO] |  +- org.springframework:spring-test:jar:5.1.4.RELEASE:test
    [INFO] |  \- org.xmlunit:xmlunit-core:jar:2.6.2:test
    [INFO] +- org.springframework:spring-context-support:jar:5.1.4.RELEASE:compile
    [INFO] |  +- org.springframework:spring-beans:jar:5.1.4.RELEASE:compile
    [INFO] |  \- org.springframework:spring-context:jar:5.1.4.RELEASE:compile
    [INFO] +- javax.el:javax.el-api:jar:2.2.4:provided
    [INFO] \- org.glassfish.web:javax.el:jar:2.2.4:provided

因为javax/el/ELManager是在el-api 3.0中引入的,而tomcat7使用的是el-api 2.2所以这个class是没有的

开发时请确保您的dev和prod环境相同。

或者您可以在 pom.xml

中添加以下依赖项
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
</dependency>

spring boot 2.1.2 版本支持任何 Servlet 3.1+ 兼容容器。

Tomcat 7 不是其中之一

spring开机docs

使用 tomcat 8 或更多 http://tomcat.apache.org/whichversion.html

Spring Boot 2.1 requires a Servlet 3.1 compatible container. Tomcat 7 is a Servlet 3.0 container 所以你不能将它与 Spring Boot 2.1 一起使用。

您需要升级到 Tomcat 8.5.x 或 9.0.x.