当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Boot应用程序中嵌入Tomcat时加载外部War文件

严曜文
2023-03-14

我正在尝试获得一个Spring Bootjar,该jar具有嵌入式Tomcat服务器,以便在运行时加载一个外部war。我正在尝试使用本文中所展示的技术来实现这一点。

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);
        }
    };
}
<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 container应用程序时,container 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 Bootwar(application类扩展了SpringBootApplicationInitializer,打包到war,并为tomcat starter依赖项提供了一个提供的范围),这也会产生相同的错误。欢迎任何建议。

共有1个答案

齐俊达
2023-03-14

汤姆卡特-贾斯珀失踪了。根据Spring Boot使用的嵌入式Tomcat的版本,向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.发行版,它是8.5.5版(“tomcat-embed-jasper”的可用版本)。

要检查在将依赖项添加到Maven之后库是否存在,请打包应用程序。在打包的应用程序中,您可以在下面找到它

\boot-inf\lib\tomcat-embedd-jasper-8.5.5.jar

 类似资料: