【问题标题】:Spring-boot : Problem deploying WAR file in Tomcat 7Spring-boot:在 Tomcat 7 中部署 WAR 文件时出现问题
【发布时间】:2019-10-16 18:46:24
【问题描述】:

全部,

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

**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);
    }
}

【问题讨论】:

  • 在这个pom.xml中,mvn clean install无法将WAR部署到Tomcat7中
  • 好吧,看起来嵌入式 Tomcat 尚未从类路径中删除,即使您在 POM 中使用“提供”覆盖了默认范围。您是否尝试再次运行 mvn clean install ?
  • @Yugerten 很抱歉没有正确描述它。我运行 mvn clean install 在目标目录下生成 WAR,我手动将它放到了 tomcat 的 webapps 文件夹下。而且,我在尝试运行 startup.bat 时遇到此错误
  • 嗨@MatheusCirillo,是的,我一直在使用 mvn clean install 但在 tomcat 上部署 WAR 时仍然看到错误。
  • @Thulasi_G 你可以添加一些日志直到“Caused by: java.....”。首先,我建议使用另一个 maven 构建插件来代替 spring-boot-maven-plugin;接下来,如果您使用 Servlet.3.x。添加这个 javax.servletjavax.servlet-api3.0.1provided

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


【解决方案1】:

请在您的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>

【讨论】:

  • 我试过这个,但我遇到了另一个参考问题。收到 Error:(9, 8) java: cannot access javax.servlet.ServletException class file for javax.servlet.ServletException not found 我的带有导入的 SpringBoot 代码看起来像这样import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class SpringServerApplication extends SpringBootServletInitializer { ........... } 请告诉我如果你知道为什么会失败..
  • @Thulasi_G,尝试添加 javax.servletjavax.servlet-api3.0.1提供
  • 答案已更新。添加 3.0.1 版本,作为它的 Tomcat 7。(我说 4.0.1 的错误。Tomcat 7 不支持 Servlet 4.0.1)
  • tomcat启动器已经提供了servlet-api,因为提供了,所以不会添加。
【解决方案2】:

您不能在 Tomcat 7 上运行 Spring Boot 2.x 应用程序。由于 Spring Boot 2.x 依赖于 Spring Framework 5.x 的简单事实,它对 3.1 版具有最低 Servlet API 要求(请参阅the Whats New In Spring 5 )。

您至少需要Tomact 8.5 才能运行它(理论上是 8.0,但已被 8.5 取代)。

要修复使用 Tomcat 8.5 或降级到 Spring Boot 1.5 行。

另见:Using Spring Boot 2.0 with Tomcat 7.0.82

【讨论】:

  • 感谢您的帮助!我搬到了 Tomcat 8.5.47 并设置了 jre_home 然后我就可以部署 war 文件了。另外,我没有排除 tomcat 依赖项,而是将其用作具有范围 = 提供的依赖项
猜你喜欢
  • 2019-05-05
  • 2016-02-15
  • 2016-08-11
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
相关资源
最近更新 更多