我正在遵循使用 Spring Boot 構建應用程式的指南。我的目標是在我的 Tomcat 服務器中運行該專案。
。首先,我可以在本地的 intellij 中運行該專案。
然后,我創建了一個名為sample.war的戰爭檔案,并按照12.17.1. 創建一個可部署的戰爭檔案。然后我把war放到webapps/sample.war目錄中。然而,它未能運行。關鍵的錯誤(完整的日志請見下文):
2021-08-26 16: 40:37,453 [localhost-startStop-15] ERROR o. s.b.SpringApplication - 應用程式運行失敗
org.springframework.context.ApplicationContextException。無法啟動Web服務器;嵌套的例外是org.springframework.beans.factory.BeanCreationException。創建名稱為'formContentFilter'的bean時出錯,該bean定義在class path資源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.class] 。通過工廠方法進行的 Bean 實體化失敗;嵌套的例外是 org.springframework.beans.BeanInstantiationException。實體化 [org.springframework.boot.web.servlet.filter.OrderedFormContentFilter] 失敗。工廠方法'formContentFilter'產生了例外;嵌套的例外是java.lang.VerifyError。壞的回傳型別
Caused by: org.springframework.beans.factory.BeanCreationException: 創建名稱為'formContentFilter'的Bean時出錯,該Bean定義在class path資源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.class] 。通過工廠方法進行的 Bean 實體化失敗;嵌套的例外是 org.springframework.beans.BeanInstantiationException。實體化 [org.springframework.boot.web.servlet.filter.OrderedFormContentFilter] 失敗。工廠方法'formContentFilter'拋出了例外;嵌套例外是java.lang.VerifyError。壞的回傳型別
pom.xml:
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven. apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"。
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"/span>>
<modelVersion>/span>4.0.0</modelVersion>/span>
<parent>/span>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>/span>
<relativePath/>/span>
</parent>/span>
<groupId>com.example</groupId>
<artifactId>serving-web-content-complete</artifactId>
<packaging>war</packaging>/span>
<version>0.0.1-SNAPSHOT</version>
<name>serving-web-content-complete</name>
<description>Spring Boot的演示專案</description>
<properties>/span>
<java.version>1.8</java.version>
<maven.compiler.source>/span>1.8</maven.compiler.source>/span>
<maven.compiler.target>/span>1.8</maven.compiler.target>/span>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>/span>UTF-8</project.reporting.outputEncoding>/span>
</properties>
<dependencies>>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</依賴性>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>/span>
</dependencies>
<build>/span>
<plugins>/span>
<plugin>/span>
<groupId>org.springframework.boot</groupId>/span>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>/span>
</plugins>/span>
</build>/span>
</project>
servingwebcontentGreetingController.java:
package com.example.servingwebcontent。
import org.springframework.stereotype.Controller。
import org.springframework.ui.Model。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
model.addAttribute("name", name)。
return "greeting"。
}
ServingWebContentApplication.java
package com.example.servingwebcontent。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication[/span
public class ServingWebContentApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ServingWebContentApplication.class)。
}
public static void main(String[] args){
SpringApplication.run(ServingWebContentApplication.class, args)。
}
}
服務器資訊:
服務器版本。Apache Tomcat/8.5.20。
服務器編號。8.5.20.0
作業系統名稱。Linux
作業系統版本。5.4.0-77-generic
下面請看catalina.out的錯誤日志:
26-2021 16: 40: 35. 813 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.undeploy 取消部署背景關系 [/sample]
26-Aug-2021 16:40:35。 824 INFO [localhost-startStop-15] org. apache.catalina.startup.HostConfig.deployWAR 部署Web應用程式歸檔 [/opt/apache-tomcat-8.5.20/webapps/Sample.war]
SLF4J: 類路徑包含多個SLF4J的系結。
SLF4J: 在[jar:file:/opt/apache-tomcat-8.5. 20/lib/news-client-0.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] 。
SLF4J: 在[jar:file:/opt/apache-tomcat-8.5中找到系結。 20/webapps/sample/WEB-INF/lib/logback-classic-1.2.3.jar! /org/slf4j/impl/StaticLoggerBinder.class] 。
SLF4J: 參見http://www.slf4j.org/codes.html#multiple_bindings 以獲得解釋。。
SLF4J: 實際系結的型別是[ch.qos.logback.classic.util.ContextSelectorStaticBinder] 。
. ____ _ __ _ _
/ / ___'_ __ _(_)_ __ __ __ _
( ( )___ | '_ | '_| | '_ / _` | |
/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)
2021-08-26 16:40:37,453 [localhost-startStop-15] ERROR o.s.b.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException。無法啟動Web服務器;嵌套的例外是org.springframework.beans.factory.BeanCreationException。在類路徑資源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.class]中定義的名稱為'formContentFilter'的bean創建錯誤。通過工廠方法進行的 Bean 實體化失敗;嵌套的例外是 org.springframework.beans.BeanInstantiationException。實體化 [org.springframework.boot.web.servlet.filter.OrderedFormContentFilter] 失敗。工廠方法'formContentFilter'引發了例外;嵌套的例外是java.lang.VerifyError。錯誤的回傳型別
例外細節。
位置。
org/springframework/http/converter/jackson2ObjectMapperBuilder$SmileFactoryInitializer.create()Lcom/fasterxml/jackson/core/JsonFactory; @7: areturn
原因。
型別'com/fasterxml/jackson/dataformat/smile/SmileFactory'(當前幀,堆疊[0])不能分配給'com/fasterxml/jackson/core/JsonFactory'(來自方法簽名)。
當前框架。
bci: @7
flags: { }
locals: { 'org/springframework/http/converter/json/Jackson2ObjectMapperBuilder$SmileFactoryInitializer' }
堆疊。{ 'com/fasterxml/jackson/dataformat/smile/SmileFactory' }
位元組代碼。
0x0000000: bb00 0359 b700 04b0
在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:175)
在org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:155)
在org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:97)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:174)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:988)
在 org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1860)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
在java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
在java.lang.Thread.run(Thread.java:748)
原因是:org.springframework.beans.factory.BeanCreationException。在類路徑資源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.class]中定義的名稱為'formContentFilter'的bean創建錯誤。通過工廠方法進行的 Bean 實體化失敗;嵌套的例外是 org.springframework.beans.BeanInstantiationException。實體化 [org.springframework.boot.web.servlet.filter.OrderedFormContentFilter] 失敗。工廠方法'formContentFilter'引發了例外;嵌套的例外是java.lang.VerifyError。錯誤的回傳型別
[用更多的相同的錯誤提示] [nipped with more of the same errors] 。
26-Aug-2021 16:40:37.531 INFO [localhost-startStop-15] org.apache.catalina.startup.HostConfig.deployWAR 網路應用程式存檔 [/opt/apache-tomcat-8.5.20/webapps/sample.war] 的部署已在 [1,708] ms 內完成。
以下是我到目前為止所嘗試的:
案例1:@SpringBootApplication注解并沒有丟失
案例2:不存在。
案例2:不適用,因為我的專案是一個Web專案
。情況3:我沒有使用spring-boot-starter-webflux
我遇到了無法啟動嵌入式 Tomcat org.springframework.context.ApplicationContextException中提到的確切問題,但我們沒有相同的原因。
我曾嘗試使用com.fasterxml.jackson.core的舊版本進行添加,如java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory中提到的2.8.10,但沒有效果。
非常、非常感謝您的幫助。
uj5u.com熱心網友回復:
正如評論中所說,問題來自于一個jackson-dataformat-smile庫,你的系統管理員把它放在Tomcat的classpath中。
如果你真的不能擺脫它,你可以使用三種解決方案之一:
添加該庫。
將該庫添加到您的專案依賴中(它將覆寫 Tomcat classpath 中的庫),
將該庫添加到您的專案中。
禁用
。MappingJackson2SmileHttpMessageConverter:與之前所說的不同,SmileFactory沒有被ServiceLoader檢測到,但是如果Spring在其classpath上檢測到該工廠,則會明確地呼叫它(參見WebMvcConfigurationSupport #addDefaultMessageConverters)。) 這就是在classpath中添加一個庫而使功能"神奇地"出現的代價。幸運的是,您可以覆寫默認的訊息轉換器:只需添加一個
WebMvcConfigurer并覆寫其configureMessageConverters。你甚至可以在你的@SpringBootApplication類中這樣做:@SpringBootApplication。 public class YourSpringApplication implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<? >> converters) { //在此添加配置。 } ...你可以復制上述
addDefaultMessageConverters的一部分。通過在你的背景關系檔案中添加改變類加載器的搜索順序(參考檔案中的方法,改變你的加載器的
delegate屬性:<Context> <Loader delegate="true"/span> /> ... </Context>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313477.html
標籤:
