Tomcat 9 can't load spring boot application

三世轮回 提交于 2021-01-29 12:41:01

问题


I am deploying a spring boot application to an existing Tomcat 9 instance. When tomcat tries to load the context, I get the following error:

org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive 
    java.lang.IllegalStateException: Error starting child
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:706)
            at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:978)
            at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1848)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
            at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
            at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:773)
            at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:427)
            at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1620)
            at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:305)
            at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
            at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1151)
            at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1353)
            at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1357)
            at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1335)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
            at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            at java.lang.Thread.run(Thread.java:748)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wservices]]
            at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
            ... 25 more
    Caused by: java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/l10n/LocalizableMessageFactory$ResourceBundleSupplier
            at org.glassfish.jersey.server.spring.SpringWebApplicationInitializer.onStartup(SpringWebApplicationInitializer.java:46)
            at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5128)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
            ... 26 more
    Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.l10n.LocalizableMessageFactory$ResourceBundleSupplier
            at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365)
            at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)

If I run the app by itself, it runs just fine using the embedded tomcat 9 engine.

Here's the spring dependencies I am using in pom file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>logback-classic</artifactId>
                <groupId>ch.qos.logback</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-oracle</artifactId>
        <version>1.0.0.RELEASE</version>
    </dependency>

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

Any help would be greatly appreciated.


回答1:


You‘ve packed a so-called über-jar, which contains every runtime dependency in one jar file. It also contains some classloading tweaks to get those dependency loaded from the jar. That will not work in Tomcat. You need to package as a web application archive.

You need to change

<project>
    ...
    <packaging>war</packaging>

See here for more detailed information.




回答2:


To deploy a war file into external tomcat, first you need to create war file instead of jar which is the default build file type for spring boot.

In Spring Boot, to create a WAR for deployment, we need 3 steps:

1.Extends SpringBootServletInitializer

2.Marked the embedded servlet container as provided.

3.Update packaging to war

See this link for steps in action.




回答3:


After digging a bit more, the problem was that the Jersey version being pulled in was the incorrect one. The project was pulling in Jersey 2.6 which is the wrong version for Spring Boot 2.2.5. Jersey 2.6 (which has jersey-common 2.6 where the class in question is) does not include the org/glassfish/jersey/internal/l10n/LocalizableMessageFactory$ResourceBundleSupplierclass

Once I fixed the pom file, Tomcat loaded it correctly.



来源:https://stackoverflow.com/questions/64232117/tomcat-9-cant-load-spring-boot-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!