如何在Spring Boot App中使用Tomcat Embedded在运行时加载外部战争文件

时间:2017-01-13 22:02:46

标签: maven tomcat spring-boot

我正在尝试使用嵌入式Tomcat服务器的Spring Boot jar来在运行时加载外部战争。我正在尝试使用此post中显示的技术执行此操作。

Spring Boot应用程序中的容器bean:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            try {
                Context context = tomcat.addWebapp(tomcat, "/first", "path/to/my.war");
                WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
                context.setLoader(loader);
            } catch (ServletException e) {
                throw new IllegalStateException("Failed to add webapp", e);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

外部战争的Maven依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

当我启动Spring Boot容器应用程序时,容器bean找到warfile并将其添加到类路径中,但随后抛出ClassNotFoundException:

2017-01-13 15:14:22.520  INFO 208 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/first]  : 1 Spring WebApplicationInitializers detected on classpath
2017-01-13 15:14:22.520  INFO 208 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/first]  : Marking servlet jsp as unavailable
2017-01-13 15:14:22.520 ERROR 208 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/first]  : Servlet [jsp] in web application [/first] threw load() exception

java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1050) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989) ~[tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4913) [tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5223) [tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) [tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) [tomcat-embed-core-8.5.6.jar!/:8.5.6]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_112]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_112]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_112]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_112]

我已经尝试在战争中使用org.apache.jasper.servlet.JspServlet包含jar。我也尝试过使用Spring Boot war(应用程序类扩展SpringBootApplicationInitializer,打包到war并给tomcat启动器依赖提供了一个范围),这也给出了同样的错误。接受任何建议。

1 个答案:

答案 0 :(得分:2)

Tomcat-Jasper失踪了。根据嵌入式Tomcat的版本,Spring Boot使用的版本会向pom.xml添加以下依赖项:

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>8.5.5</version>
</dependency>

对于Spring Boot 1.4.1.RELEASE,它是版本8.5.5(Available versions of "tomcat-embed-jasper")。

要在向Maven添加依赖项后检查库是否存在,请打包应用程序。在打包的应用程序中,您将在

下找到它
  

\ BOOT-INF \ lib中\ Tomcat的嵌入-碧玉8.5.5.jar