Spring-boot:在Tomcat 7中部署WAR文件时出现问题

时间:2019-10-16 18:46:24

标签: spring-boot spring-boot-maven-plugin

全部

我使用spring-boot,maven构建了一个示例应用程序,并且使用mvn spring-boot:run在我的IDE中运行良好。但是,当我尝试在Tomcat 7中将应用程序部署为WAR(mvn全新安装)时,出现以下错误。请帮助我找到问题的原因。

**INFO: validateJarFile(C:\_tools\apache-tomcat-7.0.96\webapps\spring-server-2.1.8.RELEASE\WEB-INF\lib\tomcat-embed-el-9.0.24.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/el/Expression.class Oct 16, 2019 2:31:57 PM org.apache.catalina.core.ContainerBase addChildInternal
SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/spring-server-2.1.8.RELEASE]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1018)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:994)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:662)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1127)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:2020)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)**

这是我的POM文件。

<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 https://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.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.service.data</groupId>
    <artifactId>spring-server</artifactId>
    <packaging>war</packaging>
    <name>dev-tools-server</name>
    <description>Spring Backend to access Tandem SQL/MX</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
            <scope>provided</scope>
        </dependency>       
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这是我的课程文件

@SpringBootApplication
public class SpringServerApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringServerApplication.class);
    }

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

2 个答案:

答案 0 :(得分:0)

请在您的pom.xml中,尝试删除此内容:

<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-starter-web</artifactId>
</dependency>

由此:

<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>

然后重试。

这确保您将嵌入式Tomcat从项目的依赖树中排除。

更新

添加提供给您的类路径的Servet API。 发生错误的原因是因为tomcat(具有Servlet API实现和规范作为传递依赖项)已被删除,因此您的类路径中没有Servlet API。

在使用Tomcat 7时添加3.0.1 Servlet API规范

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

答案 1 :(得分:0)

您无法在Tomcat 7上运行Spring Boot 2.x应用程序。由于一个简单的事实,即Spring Boot 2.x依赖于Spring Framework 5.x,它对3.1版的Servlet API的要求最低(请参阅{{3 }}。

您至少需要运行the Whats New In Spring 5(理论上为8.0,但已被8.5取代)。

要解决此问题,请使用Tomcat 8.5或降级到Spring Boot 1.5行。

另请参阅:Tomact 8.5