Tomcat 9 无法加载 Spring Boot 应用程序。

huangapple go评论87阅读模式
英文:

Tomcat 9 can't load spring boot application

问题

我正在将一个Spring Boot应用部署到一个已存在的Tomcat 9实例上。当Tomcat尝试加载上下文时,我收到以下错误消息:

org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive 
java.lang.IllegalStateException: Error starting child
...
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wservices]]
...
Caused by: java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/l10n/LocalizableMessageFactory$ResourceBundleSupplier
...
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.l10n.LocalizableMessageFactory$ResourceBundleSupplier
...

如果我单独运行应用程序,它会使用嵌入的Tomcat 9引擎正常运行。

以下是我在pom文件中使用的Spring依赖项:

<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>

任何帮助将不胜感激。

英文:

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:

&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;2.2.5.RELEASE&lt;/version&gt;
	&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

		&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-jersey&lt;/artifactId&gt;
		&lt;exclusions&gt;
			&lt;exclusion&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
			&lt;/exclusion&gt;
		&lt;/exclusions&gt;
	&lt;/dependency&gt;

	
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;exclusions&gt;
			&lt;exclusion&gt;
				&lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
				&lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
			&lt;/exclusion&gt;
		&lt;/exclusions&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
		&lt;artifactId&gt;spring-data-oracle&lt;/artifactId&gt;
		&lt;version&gt;1.0.0.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;

Any help would be greatly appreciated.

答案1

得分: 0

你已经打包了一个所谓的“uber-jar”,其中包含了所有的运行时依赖,存储在一个JAR文件中。它还包含了一些类加载调整,以从该JAR中加载这些依赖项。但在Tomcat中,这样做是行不通的。你需要将其打包成一个Web应用程序归档文件(WAR)。

你需要修改:

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

更详细的信息请参阅这里

英文:

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

&lt;project&gt;
    ...
    &lt;packaging&gt;war&lt;/packaging&gt;

See here for more detailed information.

答案2

得分: 0

在进一步深入调查后,问题是引入的 Jersey 版本不正确。项目引入了错误的 Jersey 2.6 版本,而这个版本不适用于 Spring Boot 2.2.5。Jersey 2.6(其中包含了目标类所在的 jersey-common 2.6)并不包含 org/glassfish/jersey/internal/l10n/LocalizableMessageFactory$ResourceBundleSupplier 类。

在我修复了 pom 文件后,Tomcat 正确加载了它。

英文:

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.

答案3

得分: -1

在Spring Boot中,为了创建用于部署的WAR包,我们需要3个步骤:

  1. 扩展SpringBootServletInitializer

  2. 将嵌入式Servlet容器标记为提供的。

  3. 将打包方式更新为war

有关操作步骤,请参阅此链接

英文:

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.

huangapple
  • 本文由 发表于 2020年10月7日 02:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64232117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定