【问题标题】:Crash deploying Spring-Boot WAR in Tomcat在 Tomcat 中部署 Spring-Boot WAR 时崩溃
【发布时间】:2014-01-04 15:39:18
【问题描述】:

我设法使用java -jar 命令运行了一个spring-boot Web 应用程序。但是在将打包字段改成war(在pom.xml中)并构建成war时,在tomcat 7中运行失败。

我将 pom.xml 的父级设置为:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>0.5.0.M7</version>
</parent>

还有:

public class ApplicationWebXml extends SpringBootServletInitializer {

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

}

我得到的错误是:

INFO: Deploying web application archive /home/ichsan/coding/java/tools/apache-tomcat-7.0.39/webapps/hello.war
Jan 4, 2014 10:18:50 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/home/ichsan/coding/java/tools/apache-tomcat-7.0.39/webapps/hello/WEB-INF/lib/tomcat-embed-core-7.0.47.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Jan 4, 2014 10:18:50 PM org.apache.catalina.startup.ContextConfig getServletContainerInitializer
SEVERE: The ServletContentInitializer [org.apache.tomcat.websocket.server.WsSci] could not be created
java.lang.ClassNotFoundException: org.apache.tomcat.websocket.server.WsSci
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)

我的问题是:

  1. 这里会发生什么以及如何克服这个问题?
  2. 使用spring-boot真的安全吗?因为嵌入式服务器库似乎会影响其部署到 j2ee 容器中的可靠性。我知道,它仍然是 Milestone 版本。

【问题讨论】:

  • Tomcat 的哪个版本?看起来不支持正确的 websockets。

标签: java spring-mvc spring-boot


【解决方案1】:

使用以下行修改您的pom.xml

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

这将确保在您部署到服务器时不会将嵌入式 tomcat 服务器提供给您的网络应用程序。

【讨论】:

    【解决方案2】:

    对我来说,这看起来像是一个糟糕的 WAR 文件(嵌入的 tomcat jar 根本不应该在 WEB-INF/lib 中)。您只需要在构建配置中将 tomcat 嵌入式 jar 标记为scope=providedGS guide for converting JAR to WAR 是标准配置选项的一个很好的参考,Spring Boot 代码库中有几个示例。

    如果您的应用没有使用 tomcat 的 web socket 功能,它应该仍然可以与旧版本一起使用(所以请随时在github 中提出问题)。最简单的解决方法是简单地升级 tomcat(7.0.47 应该可以)。

    【讨论】:

      【解决方案3】:

      按照 Gradle 文档中的建议,在 Gradle 中使用 providedRuntime 将 tomcat jar 移动到 WEB-INF/lib-provided 文件夹

      dependencies {
          implementation 'org.springframework.boot:spring-boot-starter-web'
          providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-10
        • 2018-12-05
        • 2016-08-11
        • 2019-03-14
        • 2018-05-18
        • 2015-09-03
        • 2020-06-17
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多